[Mlir-commits] [mlir] [MLIR][Target/Cpp] Natural induction variable naming. (PR #136102)

Niklas Degener llvmlistbot at llvm.org
Mon Apr 28 00:11:36 PDT 2025


================
@@ -1245,7 +1278,29 @@ StringRef CppEmitter::getOrCreateName(Value val) {
     assert(!hasDeferredEmission(val.getDefiningOp()) &&
            "cacheDeferredOpResult should have been called on this value, "
            "update the emitOperation function.");
-    valueMapper.insert(val, formatv("v{0}", ++valueInScopeCount.top()));
+
+    valueMapper.insert(val, formatv("v{0}", ++valueCount));
+  }
+  return *valueMapper.begin(val);
+}
+
+/// Return the existing or a new name for a loop induction variable Value.
+/// Loop induction variables follow natural naming: i, j, k,...
+StringRef CppEmitter::getOrCreateName(emitc::ForOp forOp) {
+  Value val = forOp.getInductionVar();
+
+  if (!valueMapper.count(val)) {
+
+    int64_t identifier = 'i' + loopNestingLevel;
+
+    if (identifier >= 'i' && identifier <= 'z') {
+      valueMapper.insert(val,
+                         formatv("{0}_{1}", (char)identifier, ++valueCount));
----------------
ndegener-amd wrote:

C's scope rules were not used before. I suspect that general re-using of variable names increases debugging difficulty as locating the correct vars gets harder. Therefore, I stuck with the current scheme, where variables are only re-used between functions. This inconsistency leads to the valueCount being necessary to track the values, while letting the function scope reset it.

https://github.com/llvm/llvm-project/pull/136102


More information about the Mlir-commits mailing list