Possible to inject enum case before initializing payload in SIL?

The documentation for enums in SIL says that to initialize an enum at an address you must first initialize the payload at "init_enum_data_addr" and then inject the enum case with "inject_enum_addr".

Is there any way to do it in the other order?

Here's more information about why I'm asking:

One approach for implementing a new AutoDiff feature that we are working on involves building a data structure that stores an execution trace. For example:

bb0:
  ...
  cond_br %x, bb1, bb2
bb1:
  ...
  br bb3
bb2:
  ...
  br bb3
bb3:
  ...

We want to produce a transformed function that executes the original code, gathers some data about what happened, and returns an instance of:

struct BB0Data {
  ...
  let succ: BB0Succ
}

indirect enum BB0Succ {
  case BB1(BB1Data)
  case BB2(BB2Data)
}

struct BB1Data {
  ...
  let succ: BB3Data
}
struct BB2Data {
  ...
  let succ: BB3Data
}

struct BB3Data { ... }

If we could inject the enum case before initializing the payload, initializing this data structure is easy:

bb0:
  %bb0_data_addr = alloc_stack $BB0Data
  ... initialize fields of %bb0_data_addr ... 
  %bb0_succ_addr = struct_element_addr %bb0_data_addr #BB0Data.succ
  cond_br %x, bb1, bb2
bb1:
  inject_enum_addr %bb0_succ_addr #BB0Succ.BB1
  %bb1_data_addr = init_enum_data_addr %bb0_succ_addr #BB0Succ.BB1
  ... initialize fields of %bb1_data_addr ...
  %bb1_bb3_data_addr = struct_element_addr %bb1_data_addr #BB1Data.succ
  br bb3(%bb1_bb3_data_addr)
bb2:
  ... same as bb1 except do the other case of the enum ...
bb3(%bb3_data_addr):
  ... initialize fields of %bb3_data_addr ...

However, if we have to initialize the payload before injecting the enum case, then we have to wait until after bb3 runs before we can inject the enum case. At this point we have forgotten which bb we came from, so we don't know which case to inject unless we keep track of the control flow somewhere. It's certainly possible to keep track of the control flow in some sort of stack so that we can inject the appropriate case after the payload is initialized, but it's more work than injecting the case earlier.

Terms of Service

Privacy Policy

Cookie Policy