Skip to content

Commit b9d7ef7

Browse files
authored
Fix handling of integer template argument in emitc.call_opaque (#141451)
Integer attributes supplied to `emitc.call_opaque` as arguments were treated as index into the operands list. This should be the case only for the normal arguments but not for the template arguments which can't refer to SSA values. This commit updates the handling of template arguments in mlir-to-cpp by removing special handling of integer attributes.
1 parent 9e66d54 commit b9d7ef7

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,22 @@ static LogicalResult printOperation(CppEmitter &emitter,
692692
return failure();
693693
os << callOpaqueOp.getCallee();
694694

695+
// Template arguments can't refer to SSA values and as such the template
696+
// arguments which are supplied in form of attributes can be emitted as is. We
697+
// don't need to handle integer attributes specially like we do for arguments
698+
// - see below.
699+
auto emitTemplateArgs = [&](Attribute attr) -> LogicalResult {
700+
return emitter.emitAttribute(op.getLoc(), attr);
701+
};
702+
703+
if (callOpaqueOp.getTemplateArgs()) {
704+
os << "<";
705+
if (failed(interleaveCommaWithError(*callOpaqueOp.getTemplateArgs(), os,
706+
emitTemplateArgs)))
707+
return failure();
708+
os << ">";
709+
}
710+
695711
auto emitArgs = [&](Attribute attr) -> LogicalResult {
696712
if (auto t = dyn_cast<IntegerAttr>(attr)) {
697713
// Index attributes are treated specially as operand index.
@@ -711,14 +727,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
711727
return success();
712728
};
713729

714-
if (callOpaqueOp.getTemplateArgs()) {
715-
os << "<";
716-
if (failed(interleaveCommaWithError(*callOpaqueOp.getTemplateArgs(), os,
717-
emitArgs)))
718-
return failure();
719-
os << ">";
720-
}
721-
722730
os << "(";
723731

724732
LogicalResult emittedArgs =

mlir/test/Target/Cpp/common-cpp.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,11 @@ func.func @apply() -> !emitc.ptr<i32> {
109109
func.func @array_type(%arg0: !emitc.array<3xi32>, %arg1: !emitc.array<10x20xf32>) {
110110
return
111111
}
112+
113+
// CHECK: call_opaque_with_template_arg
114+
func.func @call_opaque_with_template_arg() {
115+
emitc.call_opaque "init_tile"() {template_args = [512 : index]} : () -> ()
116+
// CHECK-NEXT: init_tile<512>();
117+
// CHECK-NEXT: return
118+
return
119+
}

0 commit comments

Comments
 (0)