[mlir][emitc] Support 'emitc::LValueType' in 'emitc::VerbatimOp' (#144151)

This PR introduces support for `emitc::LvalueType` in
`emitc::VerbatimOp`, providing a mechanism to reduce the number of
operations required when working with verbatim operations whose
arguments are of type `emitc::LvalueType`.

Before:
```mlir
emitc.func @foo() {
  %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32>
  %loaded_a = load %a : !emitc.lvalue<i32>
  emitc.verbatim "{} + {};" args %loaded_a, %loaded_a : i32, i32

  return
}
```

After:
```mlir
emitc.func @bar() {
  %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32>
  emitc.verbatim "{} + {};" args %a, %a : !emitc.lvalue<i32>, !emitc.lvalue<i32>

  return
}
```

You can now write something like this:
```mlir
emitc.func @baz() {
  %a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32>
  emitc.verbatim "++{};" args %a : !emitc.lvalue<i32>

  return
}
```
This commit is contained in:
Andrey Timonin
2025-06-16 19:37:39 +05:00
committed by GitHub
parent 39ad3151e0
commit 595a273d92
2 changed files with 13 additions and 5 deletions

View File

@@ -1304,7 +1304,7 @@ def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
}];
let arguments = (ins StrAttr:$value, Variadic<EmitCType>:$fmtArgs);
let arguments = (ins StrAttr:$value, Variadic<AnyTypeOf<[EmitCType, EmitC_LValueType]>>:$fmtArgs);
let builders = [OpBuilder<(ins "::mlir::StringAttr":$value),
[{ build($_builder, $_state, value, {}); }]>];

View File

@@ -246,12 +246,20 @@ emitc.verbatim "typedef float f32;"
// The value is not interpreted as format string if there are no operands.
emitc.verbatim "{} { }"
func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32) {
func.func @test_verbatim(%arg0 : !emitc.ptr<i32>, %arg1 : i32, %arg2: !emitc.array<3x!emitc.ptr<i32>>) {
%a = "emitc.variable"() <{value = #emitc.opaque<"1">}> : () -> !emitc.lvalue<i32>
// Check that the lvalue type can be used by verbatim.
emitc.verbatim "++{};" args %a : !emitc.lvalue<i32>
// Check that the array type can be used by verbatim.
emitc.verbatim "*{}[0] = 1;" args %arg2 : !emitc.array<3x!emitc.ptr<i32>>
emitc.verbatim "{} + {};" args %arg0, %arg1 : !emitc.ptr<i32>, i32
// Check there is no ambiguity whether %a is the argument to the emitc.verbatim op.
emitc.verbatim "a"
%a = "emitc.constant"(){value = 42 : i32} : () -> i32
// Check there is no ambiguity whether %b is the argument to the emitc.verbatim op.
emitc.verbatim "b"
%b = "emitc.constant"(){value = 42 : i32} : () -> i32
return
}