Skip to content

[mlir][emitc] Inline constant when translate #143485

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,24 @@ static bool shouldBeInlined(ExpressionOp expressionOp) {
return !user->hasTrait<OpTrait::emitc::CExpression>();
}

/// Determine whether constant \p constantOp should be emitted inline, i.e.
/// as part of its user. This function Only inline constant with one use.
static bool shouldBeInlined(ConstantOp constantOp) {
// Do not inline expressions with multiple uses.
Value result = constantOp.getResult();
if (!result.hasOneUse())
return false;

Operation *user = *result.getUsers().begin();

// Do not inline expressions used by operations with deferred emission, since
// their translation requires the materialization of variables.
if (hasDeferredEmission(user))
return false;

return true;
}

static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
Attribute value) {
OpResult result = operation->getResult(0);
Expand Down Expand Up @@ -368,6 +386,9 @@ static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,

static LogicalResult printOperation(CppEmitter &emitter,
emitc::ConstantOp constantOp) {
if (shouldBeInlined(constantOp))
return success();

Operation *operation = constantOp.getOperation();
Attribute value = constantOp.getValue();

Expand Down Expand Up @@ -1454,6 +1475,11 @@ LogicalResult CppEmitter::emitOperand(Value value) {
if (expressionOp && shouldBeInlined(expressionOp))
return emitExpression(expressionOp);

auto constantOp = dyn_cast_if_present<ConstantOp>(value.getDefiningOp());
if (constantOp && shouldBeInlined(constantOp)) {
return emitAttribute(constantOp.getLoc(), constantOp.getValue());
}

os << getOrCreateName(value);
return success();
}
Expand Down Expand Up @@ -1650,7 +1676,9 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {

if (getEmittedExpression() ||
(isa<emitc::ExpressionOp>(op) &&
shouldBeInlined(cast<emitc::ExpressionOp>(op))))
shouldBeInlined(cast<emitc::ExpressionOp>(op))) ||
(isa<emitc::ConstantOp>(op) &&
shouldBeInlined(cast<emitc::ConstantOp>(op))))
return success();

// Never emit a semicolon for some operations, especially if endening with
Expand Down
22 changes: 6 additions & 16 deletions mlir/test/Target/Cpp/for.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,13 @@ func.func @test_for_yield() {
return
}
// CPP-DEFAULT: void test_for_yield() {
// CPP-DEFAULT-NEXT: size_t [[START:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: size_t [[STOP:[^ ]*]] = 10;
// CPP-DEFAULT-NEXT: size_t [[STEP:[^ ]*]] = 1;
// CPP-DEFAULT-NEXT: int32_t [[S0:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: float [[P0:[^ ]*]] = 1.000000000e+00f;
// CPP-DEFAULT-NEXT: int32_t [[SE:[^ ]*]];
// CPP-DEFAULT-NEXT: float [[PE:[^ ]*]];
// CPP-DEFAULT-NEXT: int32_t [[SI:[^ ]*]];
// CPP-DEFAULT-NEXT: float [[PI:[^ ]*]];
// CPP-DEFAULT-NEXT: [[SI:[^ ]*]] = [[S0]];
// CPP-DEFAULT-NEXT: [[PI:[^ ]*]] = [[P0]];
// CPP-DEFAULT-NEXT: for (size_t [[ITER:[^ ]*]] = [[START]]; [[ITER]] < [[STOP]]; [[ITER]] += [[STEP]]) {
// CPP-DEFAULT-NEXT: [[SI:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: [[PI:[^ ]*]] = 1.000000000e+00f;
// CPP-DEFAULT-NEXT: for (size_t [[ITER:[^ ]*]] = 0; [[ITER]] < 10; [[ITER]] += 1) {
// CPP-DEFAULT-NEXT: int32_t [[SI_LOAD:[^ ]*]] = [[SI]];
// CPP-DEFAULT-NEXT: int32_t [[SN:[^ ]*]] = add([[SI_LOAD]], [[ITER]]);
// CPP-DEFAULT-NEXT: float [[PI_LOAD:[^ ]*]] = [[PI]];
Expand Down Expand Up @@ -104,18 +99,13 @@ func.func @test_for_yield() {
// CPP-DECLTOP-NEXT: float [[PN:[^ ]*]];
// CPP-DECLTOP-NEXT: int32_t [[SI_LOAD2:[^ ]*]];
// CPP-DECLTOP-NEXT: float [[PI_LOAD2:[^ ]*]];
// CPP-DECLTOP-NEXT: [[START]] = 0;
// CPP-DECLTOP-NEXT: [[STOP]] = 10;
// CPP-DECLTOP-NEXT: [[STEP]] = 1;
// CPP-DECLTOP-NEXT: [[S0]] = 0;
// CPP-DECLTOP-NEXT: [[P0]] = 1.000000000e+00f;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: [[SI]] = [[S0]];
// CPP-DECLTOP-NEXT: [[PI]] = [[P0]];
// CPP-DECLTOP-NEXT: for (size_t [[ITER:[^ ]*]] = [[START]]; [[ITER]] < [[STOP]]; [[ITER]] += [[STEP]]) {
// CPP-DECLTOP-NEXT: [[SI]] = 0;
// CPP-DECLTOP-NEXT: [[PI]] = 1.000000000e+00f;
// CPP-DECLTOP-NEXT: for (size_t [[ITER:[^ ]*]] = 0; [[ITER]] < 10; [[ITER]] += 1) {
// CPP-DECLTOP-NEXT: [[SI_LOAD]] = [[SI]];
// CPP-DECLTOP-NEXT: [[SN]] = add([[SI_LOAD]], [[ITER]]);
// CPP-DECLTOP-NEXT: [[PI_LOAD]] = [[PI]];
Expand Down
3 changes: 1 addition & 2 deletions mlir/test/Target/Cpp/lvalue.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ emitc.func @lvalue_variables(%v1: i32, %v2: i32) -> i32 {
// CHECK-NEXT: int32_t* [[VAR_PTR:[^ ]*]] = &[[VAR]];
// CHECK-NEXT: zero([[VAR_PTR]]);
// CHECK-NEXT: int32_t [[VAR_LOAD:[^ ]*]] = [[VAR]];
// CHECK-NEXT: int32_t [[NEG_ONE:[^ ]*]] = -1;
// CHECK-NEXT: [[VAR]] = [[NEG_ONE]];
// CHECK-NEXT: [[VAR]] = -1;
// CHECK-NEXT: return [[VAR_LOAD]];


Expand Down
14 changes: 4 additions & 10 deletions mlir/test/Target/Cpp/stdops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ func.func @one_result() -> i32 {
return %0 : i32
}
// CPP-DEFAULT: int32_t one_result() {
// CPP-DEFAULT-NEXT: int32_t [[V0:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: return [[V0]];
// CPP-DEFAULT-NEXT: return 0;

// CPP-DECLTOP: int32_t one_result() {
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
// CPP-DECLTOP-NEXT: [[V0]] = 0;
// CPP-DECLTOP-NEXT: return [[V0]];
// CPP-DECLTOP-NEXT: return 0;


func.func @two_results() -> (i32, f32) {
Expand All @@ -59,16 +57,12 @@ func.func @two_results() -> (i32, f32) {
return %0, %1 : i32, f32
}
// CPP-DEFAULT: std::tuple<int32_t, float> two_results() {
// CPP-DEFAULT: int32_t [[V0:[^ ]*]] = 0;
// CPP-DEFAULT: float [[V1:[^ ]*]] = 1.000000000e+00f;
// CPP-DEFAULT: return std::make_tuple([[V0]], [[V1]]);
// CPP-DEFAULT: return std::make_tuple(0, 1.000000000e+00f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we emit casts to ensure that the integer literal has the right type? Otherwise this will cause template type inference to change. E.g. If V0 was int64_t here, how would the C++ code look? Maybe you can add a test case to show that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to point this, I think maybe we can use literal suffix? By example, emit constant of int64_t to 0ll?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These suffixes map to the C types (Like Long long int) and the width of these are architecture dependent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, same integer width doesn't mean the same type, so when the user of constant is a call or std::tuple and std::tie, we need emit a explicit type cast around it.


// CPP-DECLTOP: std::tuple<int32_t, float> two_results() {
// CPP-DECLTOP: int32_t [[V0:[^ ]*]];
// CPP-DECLTOP: float [[V1:[^ ]*]];
// CPP-DECLTOP: [[V0]] = 0;
// CPP-DECLTOP: [[V1]] = 1.000000000e+00f;
// CPP-DECLTOP: return std::make_tuple([[V0]], [[V1]]);
// CPP-DECLTOP: return std::make_tuple(0, 1.000000000e+00f);


func.func @single_return_statement(%arg0 : i32) -> i32 {
Expand Down
Loading
Loading