Skip to content

Fix handling of integer template argument in emitc.call_opaque #141451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,22 @@ static LogicalResult printOperation(CppEmitter &emitter,
return failure();
os << callOpaqueOp.getCallee();

// Template arguments can't refer to SSA values and as such the template
// arguments which are supplied in form of attributes can be emitted as is. We
// don't need to handle integer attributes specially like we do for arguments
// - see below.
auto emitTemplateArgs = [&](Attribute attr) -> LogicalResult {
return emitter.emitAttribute(op.getLoc(), attr);
};

if (callOpaqueOp.getTemplateArgs()) {
os << "<";
if (failed(interleaveCommaWithError(*callOpaqueOp.getTemplateArgs(), os,
emitTemplateArgs)))
return failure();
os << ">";
}

auto emitArgs = [&](Attribute attr) -> LogicalResult {
if (auto t = dyn_cast<IntegerAttr>(attr)) {
// Index attributes are treated specially as operand index.
Expand All @@ -711,14 +727,6 @@ static LogicalResult printOperation(CppEmitter &emitter,
return success();
};

if (callOpaqueOp.getTemplateArgs()) {
os << "<";
if (failed(interleaveCommaWithError(*callOpaqueOp.getTemplateArgs(), os,
emitArgs)))
return failure();
os << ">";
}

os << "(";

LogicalResult emittedArgs =
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Target/Cpp/common-cpp.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ func.func @apply() -> !emitc.ptr<i32> {
func.func @array_type(%arg0: !emitc.array<3xi32>, %arg1: !emitc.array<10x20xf32>) {
return
}

// CHECK: call_opaque_with_template_arg
func.func @call_opaque_with_template_arg() {
emitc.call_opaque "init_tile"() {template_args = [512 : index]} : () -> ()
// CHECK-NEXT: init_tile<512>();
// CHECK-NEXT: return
return
}