[CIR] Implement PromotableRegionOpInterface for `cir.if`,`cir.scope` and `cir.ternary` (#215780)
mem2reg gives up on a slot if any use lives in a nested region whose
parent op doesn't implement PromotableRegionOpInterface. No CIR op
implements it. So this doesn't promote:
```mlir
%a = cir.alloca "a" align(4) : !cir.ptr<!s32i>
%c = cir.const #cir.int<42> : !s32i
cir.store %c, %a : !s32i, !cir.ptr<!s32i>
cir.if %cond {
%v = cir.load %a : !cir.ptr<!s32i>, !s32i
cir.call @use(%v) : (!s32i) -> ()
}
```
You have to run -cir-flatten-cfg first, which is why mem2reg.cir does.
That's a lot of destruction for a value that never leaves the region.
This implements the interface for cir.if and cir.scope. Both regions are
entered straight from before the op, so they see the reaching definition
unchanged. Nothing to merge, nothing to yield.
Stores inside a region are refused. A new definition has to exit through
a result, cir.if has none, cir.scope has one it may already be using.
Supporting that means giving those ops results. Separate patch.
My use case specifically:
Downstream, I'm propagating constants into CUDA kernel arguments.[ Every
launch site sits inside a cir.if, the __cudaPushCallConfiguration
guard](https://github.com/llvm/llvm-project/blob/5bf59e2c4b54a85e4e7f0e188b99061beb2708f6/clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp#L33),
so the argument reaching the device stub call site is always a cir.load.
In particular this allows other MLIR passes like sccp, determine if such
value is in constant form through the call graph. So the argument flow
would be like this: mem2reg turns it into the block argument, sccp turns
that into a cir.const, and you get the constant at the call site.
Claude assisted with this patch.
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>