Skip to content

Conversation

aniragil
Copy link
Contributor

The translator currently implements deferred emission for certain ops. Like expressions, these ops are emitted as part of their users but unlike expressions, this is mandatory. Besides complicating the code with a second inlining mechanism, deferred emission's inlining is limited as it's not recursive.

This patch extends EmitC's expressions to deferred emission ops by (a) marking them as CExpressions, (b) extending expression interface to mark ops as always-inline and (c) support inlining of always-inline CExpressions even when not packed of an emitc.expression op, retaining current behavior.

The translator currently implements deferred emission for certain ops.
Like expressions, these ops are emitted as part of their users but
unlike expressions, this is mandatory. Besides complicating the code
with a second inlining mechanism, deferred emission's inlining is
limited as it's not recursive.

This patch extends EmitC's expressions to deferred emission ops by (a)
marking them as CExpressions, (b) extending expression interface to
mark ops as always-inline and (c) support inlining of always-inline
CExpressions even when not packed of an `emitc.expression` op,
retaining current behavior.
@llvmbot
Copy link
Member

llvmbot commented Sep 21, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-emitc

Author: Gil Rapaport (aniragil)

Changes

The translator currently implements deferred emission for certain ops. Like expressions, these ops are emitted as part of their users but unlike expressions, this is mandatory. Besides complicating the code with a second inlining mechanism, deferred emission's inlining is limited as it's not recursive.

This patch extends EmitC's expressions to deferred emission ops by (a) marking them as CExpressions, (b) extending expression interface to mark ops as always-inline and (c) support inlining of always-inline CExpressions even when not packed of an emitc.expression op, retaining current behavior.


Patch is 51.50 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/159975.diff

7 Files Affected:

  • (modified) mlir/include/mlir/Dialect/EmitC/IR/EmitC.td (+68-10)
  • (modified) mlir/include/mlir/Dialect/EmitC/IR/EmitCInterfaces.td (+23-4)
  • (modified) mlir/lib/Dialect/EmitC/IR/EmitC.cpp (+12-1)
  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+189-135)
  • (modified) mlir/test/Dialect/EmitC/form-expressions.mlir (+157-6)
  • (modified) mlir/test/Target/Cpp/expressions.mlir (+159)
  • (modified) mlir/test/Target/Cpp/member.mlir (+4-4)
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 721f9f6b320ad..ebd4850161894 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -507,7 +507,7 @@ def EmitC_ExpressionOp
 
   let arguments = (ins Variadic<AnyTypeOf<[EmitCType, EmitC_LValueType]>>:$defs,
       UnitAttr:$do_not_inline);
-  let results = (outs EmitCType:$result);
+  let results = (outs AnyTypeOf<[EmitCType, EmitC_LValueType]>:$result);
   let regions = (region SizedRegion<1>:$region);
 
   let hasVerifier = 1;
@@ -873,7 +873,7 @@ def EmitC_IncludeOp
   let hasCustomAssemblyFormat = 1;
 }
 
-def EmitC_LiteralOp : EmitC_Op<"literal", [Pure]> {
+def EmitC_LiteralOp : EmitC_Op<"literal", [Pure, CExpressionInterface]> {
   let summary = "Literal operation";
   let description = [{
     The `emitc.literal` operation produces an SSA value equal to some constant
@@ -896,6 +896,15 @@ def EmitC_LiteralOp : EmitC_Op<"literal", [Pure]> {
 
   let hasVerifier = 1;
   let assemblyFormat = "$value attr-dict `:` type($result)";
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
 def EmitC_LogicalAndOp : EmitC_BinaryOp<"logical_and", []> {
@@ -1062,7 +1071,7 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", []> {
   let hasVerifier = 1;
 }
 
-def EmitC_MemberOp : EmitC_Op<"member"> {
+def EmitC_MemberOp : EmitC_Op<"member", [CExpressionInterface]> {
   let summary = "Member operation";
   let description = [{
     With the `emitc.member` operation the member access operator `.` can be
@@ -1083,9 +1092,18 @@ def EmitC_MemberOp : EmitC_Op<"member"> {
     EmitC_LValueOf<[EmitC_OpaqueType]>:$operand
   );
   let results = (outs AnyTypeOf<[EmitC_ArrayType, EmitC_LValueType]>);
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
-def EmitC_MemberOfPtrOp : EmitC_Op<"member_of_ptr"> {
+def EmitC_MemberOfPtrOp : EmitC_Op<"member_of_ptr", [CExpressionInterface]> {
   let summary = "Member of pointer operation";
   let description = [{
     With the `emitc.member_of_ptr` operation the member access operator `->`
@@ -1108,6 +1126,15 @@ def EmitC_MemberOfPtrOp : EmitC_Op<"member_of_ptr"> {
     EmitC_LValueOf<[EmitC_OpaqueType,EmitC_PointerType]>:$operand
   );
   let results = (outs AnyTypeOf<[EmitC_ArrayType, EmitC_LValueType]>);
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
 def EmitC_ConditionalOp : EmitC_Op<"conditional",
@@ -1277,8 +1304,10 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
   let hasVerifier = 1;
 }
 
-def EmitC_GetGlobalOp : EmitC_Op<"get_global",
-    [Pure, DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
+def EmitC_GetGlobalOp
+    : EmitC_Op<"get_global", [Pure,
+                              DeclareOpInterfaceMethods<SymbolUserOpInterface>,
+                              CExpressionInterface]> {
   let summary = "Obtain access to a global variable";
   let description = [{
      The `emitc.get_global` operation retrieves the lvalue of a
@@ -1296,6 +1325,15 @@ def EmitC_GetGlobalOp : EmitC_Op<"get_global",
   let arguments = (ins FlatSymbolRefAttr:$name);
   let results = (outs AnyTypeOf<[EmitC_ArrayType, EmitC_LValueType]>:$result);
   let assemblyFormat = "$name `:` type($result) attr-dict";
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
 def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
@@ -1406,7 +1444,8 @@ def EmitC_YieldOp : EmitC_Op<"yield",
     value is yielded.
   }];
 
-  let arguments = (ins Optional<EmitCType>:$result);
+  let arguments =
+      (ins Optional<AnyTypeOf<[EmitCType, EmitC_LValueType]>>:$result);
   let builders = [OpBuilder<(ins), [{ /* nothing to do */ }]>];
 
   let hasVerifier = 1;
@@ -1477,7 +1516,7 @@ def EmitC_IfOp : EmitC_Op<"if",
   let hasCustomAssemblyFormat = 1;
 }
 
-def EmitC_SubscriptOp : EmitC_Op<"subscript", []> {
+def EmitC_SubscriptOp : EmitC_Op<"subscript", [CExpressionInterface]> {
   let summary = "Subscript operation";
   let description = [{
     With the `emitc.subscript` operation the subscript operator `[]` can be applied
@@ -1525,6 +1564,15 @@ def EmitC_SubscriptOp : EmitC_Op<"subscript", []> {
 
   let hasVerifier = 1;
   let assemblyFormat = "$value `[` $indices `]` attr-dict `:` functional-type(operands, results)";
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
 def EmitC_SwitchOp : EmitC_Op<"switch", [RecursiveMemoryEffects,
@@ -1707,8 +1755,9 @@ def EmitC_FieldOp : EmitC_Op<"field", [Symbol]> {
 }
 
 def EmitC_GetFieldOp
-    : EmitC_Op<"get_field", [Pure, DeclareOpInterfaceMethods<
-                                       SymbolUserOpInterface>]> {
+    : EmitC_Op<"get_field", [Pure,
+                             DeclareOpInterfaceMethods<SymbolUserOpInterface>,
+                             CExpressionInterface]> {
   let summary = "Obtain access to a field within a class instance";
   let description = [{
      The `emitc.get_field` operation retrieves the lvalue of a
@@ -1725,6 +1774,15 @@ def EmitC_GetFieldOp
   let results = (outs EmitCType:$result);
   let assemblyFormat = "$field_name `:` type($result) attr-dict";
   let hasVerifier = 1;
+
+  let extraClassDeclaration = [{
+    bool hasSideEffects() {
+      return false;
+    }
+    bool alwaysInline() {
+      return true; // C doesn't support variable references.
+    }
+  }];
 }
 
 #endif // MLIR_DIALECT_EMITC_IR_EMITC
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitCInterfaces.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitCInterfaces.td
index 777784e56202a..c11e017e40d0f 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitCInterfaces.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitCInterfaces.td
@@ -21,8 +21,8 @@ def CExpressionInterface : OpInterface<"CExpressionInterface"> {
   }];
 
   let cppNamespace = "::mlir::emitc";
-  let methods = [
-    InterfaceMethod<[{
+  let methods =
+      [InterfaceMethod<[{
       Check whether operation has side effects that may affect the expression
       evaluation.
 
@@ -38,9 +38,28 @@ def CExpressionInterface : OpInterface<"CExpressionInterface"> {
       };
       ```
     }],
-      "bool", "hasSideEffects", (ins), /*methodBody=*/[{}],
-       /*defaultImplementation=*/[{
+                       "bool", "hasSideEffects", (ins), /*methodBody=*/[{}],
+                       /*defaultImplementation=*/[{
         return true;
+    }]>,
+       InterfaceMethod<[{
+      Check whether operation must be inlined into all its users.
+
+      By default operation is not marked as always inlined.
+
+      ```c++
+      class ConcreteOp ... {
+      public:
+        bool alwaysInline() {
+          // That way we can override the default implementation.
+          return true;
+        }
+      };
+      ```
+    }],
+                       "bool", "alwaysInline", (ins), /*methodBody=*/[{}],
+                       /*defaultImplementation=*/[{
+        return false;
     }]>,
   ];
 }
diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index 5c8564bca6f86..4468ac686aa8b 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -483,7 +483,8 @@ LogicalResult ExpressionOp::verify() {
     Operation *op = worklist.back();
     worklist.pop_back();
     if (visited.contains(op)) {
-      if (cast<CExpressionInterface>(op).hasSideEffects())
+      auto cExpr = cast<CExpressionInterface>(op);
+      if (!cExpr.alwaysInline() && cExpr.hasSideEffects())
         return emitOpError(
             "requires exactly one use for operations with side effects");
     }
@@ -494,6 +495,12 @@ LogicalResult ExpressionOp::verify() {
       }
   }
 
+  if (getDoNotInline() &&
+      cast<emitc::CExpressionInterface>(rootOp).alwaysInline()) {
+    return emitOpError("root operation must be inlined but expression is marked"
+                       " do-not-inline");
+  }
+
   return success();
 }
 
@@ -980,6 +987,10 @@ LogicalResult emitc::YieldOp::verify() {
   if (!result && containingOp->getNumResults() != 0)
     return emitOpError() << "does not yield a value to be returned by parent";
 
+  if (result && isa<emitc::LValueType>(result.getType()) &&
+      !isa<ExpressionOp>(containingOp))
+    return emitOpError() << "yielding lvalues is not supported for this op";
+
   return success();
 }
 
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index a5bd80e9d6b8b..7541845ff6242 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -8,6 +8,7 @@
 
 #include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
 #include "mlir/Dialect/EmitC/IR/EmitC.h"
+#include "mlir/Dialect/EmitC/IR/EmitCInterfaces.h"
 #include "mlir/Dialect/Func/IR/FuncOps.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/IR/BuiltinTypes.h"
@@ -99,13 +100,18 @@ static FailureOr<int> getOperatorPrecedence(Operation *operation) {
       .Case<emitc::ConditionalOp>([&](auto op) { return 2; })
       .Case<emitc::ConstantOp>([&](auto op) { return 17; })
       .Case<emitc::DivOp>([&](auto op) { return 13; })
+      .Case<emitc::GetGlobalOp>([&](auto op) { return 18; })
+      .Case<emitc::LiteralOp>([&](auto op) { return 18; })
       .Case<emitc::LoadOp>([&](auto op) { return 16; })
       .Case<emitc::LogicalAndOp>([&](auto op) { return 4; })
       .Case<emitc::LogicalNotOp>([&](auto op) { return 15; })
       .Case<emitc::LogicalOrOp>([&](auto op) { return 3; })
+      .Case<emitc::MemberOfPtrOp>([&](auto op) { return 17; })
+      .Case<emitc::MemberOp>([&](auto op) { return 17; })
       .Case<emitc::MulOp>([&](auto op) { return 13; })
       .Case<emitc::RemOp>([&](auto op) { return 13; })
       .Case<emitc::SubOp>([&](auto op) { return 12; })
+      .Case<emitc::SubscriptOp>([&](auto op) { return 17; })
       .Case<emitc::UnaryMinusOp>([&](auto op) { return 15; })
       .Case<emitc::UnaryPlusOp>([&](auto op) { return 15; })
       .Default([](auto op) { return op->emitError("unsupported operation"); });
@@ -173,31 +179,27 @@ struct CppEmitter {
   /// Emits the operands of the operation. All operands are emitted in order.
   LogicalResult emitOperands(Operation &op);
 
-  /// Emits value as an operands of an operation
-  LogicalResult emitOperand(Value value);
+  /// Emits value as an operand of an operation. If \p isInBrackets is true,
+  /// this operand is already being emitted between some kind of brackets, so
+  /// there is no need to wrap it in parentheses for correct precedence.
+  LogicalResult emitOperand(Value value, bool isInBrackets = false);
 
-  /// Emit an expression as a C expression.
-  LogicalResult emitExpression(ExpressionOp expressionOp);
+  /// Collect all operations to emit as an expression starting at \p op,
+  /// recursively adding operands that shouldBeInlined.
+  void buildExpression(Operation *op);
 
-  /// Insert the expression representing the operation into the value cache.
-  void cacheDeferredOpResult(Value value, StringRef str);
+  /// Emit an expression as a C expression.
+  LogicalResult emitExpression(Operation *op);
 
   /// Return the existing or a new name for a Value.
   StringRef getOrCreateName(Value val);
 
+  void setName(Value val, StringRef name);
+
   /// Return the existing or a new name for a loop induction variable of an
   /// emitc::ForOp.
   StringRef getOrCreateInductionVarName(Value val);
 
-  // Returns the textual representation of a subscript operation.
-  std::string getSubscriptName(emitc::SubscriptOp op);
-
-  // Returns the textual representation of a member (of object) operation.
-  std::string createMemberAccess(emitc::MemberOp op);
-
-  // Returns the textual representation of a member of pointer operation.
-  std::string createMemberAccess(emitc::MemberOfPtrOp op);
-
   /// Return the existing or a new label of a Block.
   StringRef getOrCreateName(Block &block);
 
@@ -257,25 +259,24 @@ struct CppEmitter {
     return !fileId.empty() && file.getId() == fileId;
   }
 
-  /// Get expression currently being emitted.
-  ExpressionOp getEmittedExpression() { return emittedExpression; }
+  /// Is expression currently being emitted.
+  bool isEmittingExpression() { return !emittedExpression.empty(); }
 
   /// Determine whether given value is part of the expression potentially being
   /// emitted.
-  bool isPartOfCurrentExpression(Value value) {
-    if (!emittedExpression)
-      return false;
+  Operation *isPartOfCurrentExpression(Value value) {
     Operation *def = value.getDefiningOp();
-    if (!def)
-      return false;
-    return isPartOfCurrentExpression(def);
+    if (def)
+      return isPartOfCurrentExpression(def) ? def : nullptr;
+    return nullptr;
   }
 
   /// Determine whether given operation is part of the expression potentially
   /// being emitted.
   bool isPartOfCurrentExpression(Operation *def) {
-    auto operandExpression = dyn_cast<ExpressionOp>(def->getParentOp());
-    return operandExpression && operandExpression == emittedExpression;
+    if (auto parentExpression = dyn_cast<ExpressionOp>(def->getParentOp()))
+      def = parentExpression;
+    return emittedExpression.contains(def);
   };
 
   // Resets the value counter to 0.
@@ -322,7 +323,7 @@ struct CppEmitter {
   unsigned int valueCount{0};
 
   /// State of the current expression being emitted.
-  ExpressionOp emittedExpression;
+  SmallPtrSet<Operation *, 16> emittedExpression;
   SmallVector<int> emittedExpressionPrecedence;
 
   void pushExpressionPrecedence(int precedence) {
@@ -338,19 +339,35 @@ struct CppEmitter {
 };
 } // namespace
 
-/// Determine whether expression \p op should be emitted in a deferred way.
-static bool hasDeferredEmission(Operation *op) {
-  return isa_and_nonnull<emitc::GetGlobalOp, emitc::LiteralOp, emitc::MemberOp,
-                         emitc::MemberOfPtrOp, emitc::SubscriptOp,
-                         emitc::GetFieldOp>(op);
-}
-
-/// Determine whether expression \p expressionOp should be emitted inline, i.e.
-/// as part of its user. This function recommends inlining of any expressions
-/// that can be inlined unless it is used by another expression, under the
-/// assumption that  any expression fusion/re-materialization was taken care of
+/// Determine whether operation \p operation should be emitted inline, i.e.
+/// as part of its user.
+/// The operation can force inlining using its
+/// CExpressionInterface::alwaysInline() method when it's not included in any
+/// ExpressionOp or is some ExpressionOp's root operation.
+/// Otherwise, for any ExpressionOp that can be inlined, this function
+/// recommends inlining unless it is used by another expression, under the
+/// assumption that any expression fusion/re-materialization was taken care of
 /// by transformations run by the backend.
-static bool shouldBeInlined(ExpressionOp expressionOp) {
+static bool shouldBeInlined(Operation *operation) {
+  auto expressionInterface = dyn_cast<CExpressionInterface>(operation);
+
+  // Inline if this is an always-inline CExpression not part of any
+  // expression.
+  if (expressionInterface && expressionInterface.alwaysInline()) {
+    assert(!isa<ExpressionOp>(operation->getParentOp()) &&
+           "Unexpectedly called on operation included in expression");
+    return true;
+  }
+
+  ExpressionOp expressionOp = dyn_cast<ExpressionOp>(operation);
+
+  if (!expressionOp)
+    return false;
+
+  // Inline if the root operation is an always-inline CExpression.
+  if (cast<CExpressionInterface>(expressionOp.getRootOp()).alwaysInline())
+    return true;
+
   // Do not inline if expression is marked as such.
   if (expressionOp.getDoNotInline())
     return false;
@@ -367,17 +384,83 @@ static bool shouldBeInlined(ExpressionOp expressionOp) {
 
   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;
-
   // Do not inline expressions used by other expressions or by ops with the
   // CExpressionInterface. If this was intended, the user could have been merged
   // into the expression op.
   return !isa<emitc::ExpressionOp, emitc::CExpressionInterface>(*user);
 }
 
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::GetFieldOp getFieldOp) {
+  if (!emitter.isPartOfCurrentExpression(getFieldOp.getOperation())) {
+    emitter.setName(getFieldOp.getResult(), getFieldOp.getFieldName());
+    return success();
+  }
+
+  emitter.ostream() << getFieldOp.getFieldName();
+  return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::GetGlobalOp getGlobalOp) {
+  if (!emitter.isPartOfCurrentExpression(getGlobalOp.getOperation())) {
+    emitter.setName(getGlobalOp.getResult(), getGlobalOp.getName());
+    return success();
+  }
+
+  emitter.ostream() << getGlobalOp.getName();
+  return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::LiteralOp literalOp) {
+  if (!emitter.isPartOfCurrentExpression(literalOp.getOperation()))
+    return success();
+
+  emitter.ostream() << literalOp.getValue();
+  return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::MemberOp memberOp) {
+  if (!emitter.isPartOfCurrentExpression(memberOp.getOperation()))
+    return success();
+
+  if (failed(emitter.emitOperand(memberOp.getOperand())))
+    return failure();
+  emitter.ostream() << "." << memberOp.getMember();
+  return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::MemberOfPtrOp memberOfPtrOp) {
+  if (!emitter.isPartOfCurrentExpression(memberOfPtrOp.getOperation()))
+    return success();
+
+  if (failed(emitter.emitOperand(memberOfPtrOp.getOperand())))
+    return failure();
+  emitter.ostream() << "->" << memberOfPtrOp.getMember();
+  return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+                                    emitc::SubscriptOp subscriptOp) {
+  if (!emitter.isPartOfCurrentExpression(subscriptOp.getOperation())) {
+    return success();
+  }
+
+  raw_ostream &os = emitter.ostream();
+  if (failed(emitter.emitOperand(subscriptOp.getValue())))
+    return failure();
+  for (auto index : subscriptOp.getIndices()) {
+    os << "[";
+    if (failed(emitter.emitOperand(index, /*isInBrackets=*/true)))
+      return failure();
+    os << "]";
+  }
+  return success();
+}
+
 static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
                                      Attribute value) {
   OpResult result = operation->getResult(0);
@@ -437,11 +520,11 @@ static LogicalResult printOperation(CppEmitter &emitter,
 
 static LogicalResult printOperation(CppEmitter &emitter,
                                     emitc::AssignOp assignOp) {
-  OpResult result = assignOp.getVar().getDefiningOp()->getResult(0);
-
-  if (failed(emitter.emitVariableAssignment(result)))
+  if (failed(emitter.emitOperand(assignOp.getVar())))
     return failure();
 
+  emitter.ostream() << " = ";
+
   return emitter.emitOperand(assignOp.getValue());
 }
 
@@ -1288,44 +1371,14 @@ CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop,
   labelInScopeCount.push(0);
 }
 
-std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {
-  std::string out;
-  llvm::raw_string_ostream ss(out);
-  ss << getOrCreateName(op.getValue());
-  for (auto index : op.getIndices()) {
-    ss << "[" << getOrCreateName(index) << "]";
-  }
-  return out;
-}
-
-std::string CppEmitter::createMemberAccess(emitc::Membe...
[truncated]

@aniragil
Copy link
Contributor Author

@Vladislave0-0 this patch also addresses literals, per your patch.

Copy link
Member

@marbre marbre left a comment

Choose a reason for hiding this comment

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

It would be good to get some more eyes on this as it is changes quite some lines :)

Comment on lines -1317 to +1374
void CppEmitter::cacheDeferredOpResult(Value value, StringRef str) {
if (!valueMapper.count(value))
valueMapper.insert(value, str.str());
void CppEmitter::setName(Value val, StringRef name) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong preferrence with regards to bring back the name cacheDeferredOpResult but maybe we find something more descriptive than setName?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's meant to match the existing getOrCreateName(), which is also used for block labels (we now also have getOrCreateInductionVarName()). We should indeed find something less obscure for that mapping-to-C API, but perhaps do it in a separate patch?

Copy link
Member

Choose a reason for hiding this comment

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

Fine to address in a follow up, to me it was just not obvious where this comes from but the reference to getOrCreateName() makes sense of course.

Comment on lines +34 to +36
// CPP-DEFAULT-NEXT: int32_t [[V5:[^ ]*]] = ([[V2]].c)[[[Index]]];
// CPP-DEFAULT-NEXT: [[V4]] = [[V5]];
// CPP-DEFAULT-NEXT: [[V2]].d[[[Index]]] = [[V1]];
// CPP-DEFAULT-NEXT: ([[V2]].d)[[[Index]]] = [[V1]];
Copy link
Member

Choose a reason for hiding this comment

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

Is this desired or rather a side effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a side effect: When emitting expressions we currently don't omit parentheses when operator precedence is equal and associativity is left-to-right, as is the case for . and [].

@aniragil
Copy link
Contributor Author

aniragil commented Oct 2, 2025

It would be good to get some more eyes on this as it is changes quite some lines :)

@simon-camp, @mgehre-amd could you also take a look?

}],
"bool", "hasSideEffects", (ins), /*methodBody=*/[{}],
/*defaultImplementation=*/[{
"bool", "hasSideEffects", (ins), /*methodBody=*/[{}],
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants