[flang-commits] [flang] [flang][NFC] Move the rest of ops creation to new APIs (PR #152079)

via flang-commits flang-commits at lists.llvm.org
Mon Aug 4 22:21:03 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-fir-hlfir

Author: Valentin Clement (バレンタイン クレメン) (clementval)

<details>
<summary>Changes</summary>

See https://github.com/llvm/llvm-project/pull/147168

This should be the last patch to move all the rest of ops creation to the new APIs in flang. 

---

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


11 Files Affected:

- (modified) flang/include/flang/Optimizer/Builder/FIRBuilder.h (+6-5) 
- (modified) flang/include/flang/Optimizer/Builder/Factory.h (+21-21) 
- (modified) flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td (+2-2) 
- (modified) flang/lib/Lower/Bridge.cpp (+51-46) 
- (modified) flang/lib/Lower/OpenMP/OpenMP.cpp (+40-39) 
- (modified) flang/lib/Optimizer/Builder/FIRBuilder.cpp (+15-13) 
- (modified) flang/lib/Optimizer/Builder/IntrinsicCall.cpp (+8-10) 
- (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+7-7) 
- (modified) flang/lib/Optimizer/CodeGen/TargetRewrite.cpp (+10-9) 
- (modified) flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp (+2-2) 
- (modified) flang/unittests/Optimizer/FortranVariableTest.cpp (+5-5) 


``````````diff
diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
index 8b4c2f73a84e8..d8b6a9f87ab19 100644
--- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
@@ -549,8 +549,9 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
   }
 
   mlir::Value genNot(mlir::Location loc, mlir::Value boolean) {
-    return create<mlir::arith::CmpIOp>(loc, mlir::arith::CmpIPredicate::eq,
-                                       boolean, createBool(loc, false));
+    return mlir::arith::CmpIOp::create(*this, loc,
+                                       mlir::arith::CmpIPredicate::eq, boolean,
+                                       createBool(loc, false));
   }
 
   /// Generate code testing \p addr is not a null address.
@@ -641,7 +642,7 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
   mlir::Value createUnsigned(mlir::Location loc, mlir::Type resultType,
                              mlir::Value left, mlir::Value right) {
     if (!resultType.isIntOrFloat())
-      return create<OpTy>(loc, resultType, left, right);
+      return OpTy::create(*this, loc, resultType, left, right);
     mlir::Type signlessType = mlir::IntegerType::get(
         getContext(), resultType.getIntOrFloatBitWidth(),
         mlir::IntegerType::SignednessSemantics::Signless);
@@ -654,7 +655,7 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
       right = createConvert(loc, signlessType, right);
       opResType = signlessType;
     }
-    mlir::Value result = create<OpTy>(loc, opResType, left, right);
+    mlir::Value result = OpTy::create(*this, loc, opResType, left, right);
     if (resultType.isUnsignedInteger())
       result = createConvert(loc, resultType, result);
     return result;
@@ -666,7 +667,7 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
                             mlir::Value ptr1, mlir::Value ptr2) {
     ptr1 = createConvert(loc, getIndexType(), ptr1);
     ptr2 = createConvert(loc, getIndexType(), ptr2);
-    return create<mlir::arith::CmpIOp>(loc, predicate, ptr1, ptr2);
+    return mlir::arith::CmpIOp::create(*this, loc, predicate, ptr1, ptr2);
   }
 
 private:
diff --git a/flang/include/flang/Optimizer/Builder/Factory.h b/flang/include/flang/Optimizer/Builder/Factory.h
index e67ef457c8b4c..fd9f33c340fa9 100644
--- a/flang/include/flang/Optimizer/Builder/Factory.h
+++ b/flang/include/flang/Optimizer/Builder/Factory.h
@@ -53,8 +53,8 @@ void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
     fir::StoreOp::create(builder, loc, load, dst);
     return;
   }
-  auto zero = builder.template create<mlir::arith::ConstantIndexOp>(loc, 0);
-  auto one = builder.template create<mlir::arith::ConstantIndexOp>(loc, 1);
+  auto zero = mlir::arith::ConstantIndexOp::create(builder, loc, 0);
+  auto one = mlir::arith::ConstantIndexOp::create(builder, loc, 1);
   auto toArrayTy = [&](fir::CharacterType ty) {
     return fir::ReferenceType::get(fir::SequenceType::get(
         fir::SequenceType::ShapeRef{fir::SequenceType::getUnknownExtent()},
@@ -68,8 +68,8 @@ void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
     return fir::ReferenceType::get(toEleTy(ty));
   };
   if (!srcLen && !dstLen && srcTy.getLen() >= dstTy.getLen()) {
-    auto upper = builder.template create<mlir::arith::ConstantIndexOp>(
-        loc, dstTy.getLen() - 1);
+    auto upper =
+        mlir::arith::ConstantIndexOp::create(builder, loc, dstTy.getLen() - 1);
     auto loop = fir::DoLoopOp::create(builder, loc, zero, upper, one);
     auto insPt = builder.saveInsertionPoint();
     builder.setInsertionPointToStart(loop.getBody());
@@ -92,26 +92,26 @@ void genCharacterCopy(mlir::Value src, mlir::Value srcLen, mlir::Value dst,
     return;
   }
   auto minusOne = [&](mlir::Value v) -> mlir::Value {
-    return builder.template create<mlir::arith::SubIOp>(
-        loc, fir::ConvertOp::create(builder, loc, one.getType(), v), one);
+    return mlir::arith::SubIOp::create(
+        builder, loc, fir::ConvertOp::create(builder, loc, one.getType(), v),
+        one);
   };
   mlir::Value len = dstLen ? minusOne(dstLen)
-                           : builder
-                                 .template create<mlir::arith::ConstantIndexOp>(
-                                     loc, dstTy.getLen() - 1)
+                           : mlir::arith::ConstantIndexOp::create(
+                                 builder, loc, dstTy.getLen() - 1)
                                  .getResult();
   auto loop = fir::DoLoopOp::create(builder, loc, zero, len, one);
   auto insPt = builder.saveInsertionPoint();
   builder.setInsertionPointToStart(loop.getBody());
   mlir::Value slen =
-      srcLen ? fir::ConvertOp::create(builder, loc, one.getType(), srcLen)
-                   .getResult()
-             : builder
-                   .template create<mlir::arith::ConstantIndexOp>(
-                       loc, srcTy.getLen())
-                   .getResult();
-  auto cond = builder.template create<mlir::arith::CmpIOp>(
-      loc, mlir::arith::CmpIPredicate::slt, loop.getInductionVar(), slen);
+      srcLen
+          ? fir::ConvertOp::create(builder, loc, one.getType(), srcLen)
+                .getResult()
+          : mlir::arith::ConstantIndexOp::create(builder, loc, srcTy.getLen())
+                .getResult();
+  auto cond =
+      mlir::arith::CmpIOp::create(builder, loc, mlir::arith::CmpIPredicate::slt,
+                                  loop.getInductionVar(), slen);
   auto ifOp = fir::IfOp::create(builder, loc, cond, /*withElse=*/true);
   builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
   auto csrcTy = toArrayTy(srcTy);
@@ -189,7 +189,7 @@ originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
     auto ty = fir::dyn_cast_ptrOrBoxEleTy(memTy);
     assert(ty && mlir::isa<fir::SequenceType>(ty));
     auto seqTy = mlir::cast<fir::SequenceType>(ty);
-    auto one = builder.template create<mlir::arith::ConstantIndexOp>(loc, 1);
+    auto one = mlir::arith::ConstantIndexOp::create(builder, loc, 1);
     const auto dimension = seqTy.getDimension();
     if (shapeVal) {
       assert(dimension == mlir::cast<fir::ShapeOp>(shapeVal.getDefiningOp())
@@ -200,7 +200,7 @@ originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
       if (i.index() < dimension) {
         assert(fir::isa_integer(i.value().getType()));
         result.push_back(
-            builder.template create<mlir::arith::AddIOp>(loc, i.value(), one));
+            mlir::arith::AddIOp::create(builder, loc, i.value(), one));
       } else {
         result.push_back(i.value());
       }
@@ -211,8 +211,8 @@ originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
   unsigned origOff = 0;
   for (auto i : llvm::enumerate(indices)) {
     if (i.index() < dimension)
-      result.push_back(builder.template create<mlir::arith::AddIOp>(
-          loc, i.value(), origins[origOff++]));
+      result.push_back(mlir::arith::AddIOp::create(builder, loc, i.value(),
+                                                   origins[origOff++]));
     else
       result.push_back(i.value());
   }
diff --git a/flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td b/flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td
index 2414de496d45b..8341720d34c1e 100644
--- a/flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td
+++ b/flang/include/flang/Optimizer/Dialect/CanonicalizationPatterns.td
@@ -105,8 +105,8 @@ def CombineConvertTruncOptPattern
            (IntPred $arg, $irm), (SmallerWidthPred $arg, $irm)]>;
 
 def createConstantOp
-    : NativeCodeCall<"$_builder.create<mlir::arith::ConstantOp>"
-                     "($_loc, $_builder.getIndexType(), "
+    : NativeCodeCall<"mlir::arith::ConstantOp::create"
+                     "($_builder, $_loc, $_builder.getIndexType(), "
                      "rewriter.getIndexAttr("
                      "mlir::dyn_cast<mlir::IntegerAttr>($1).getInt()))">;
 
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 61476efbee1fc..c56de2d4d1e1f 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1458,7 +1458,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
 
   void genBranch(mlir::Block *targetBlock) {
     assert(targetBlock && "missing unconditional target block");
-    builder->create<mlir::cf::BranchOp>(toLocation(), targetBlock);
+    mlir::cf::BranchOp::create(*builder, toLocation(), targetBlock);
   }
 
   void genConditionalBranch(mlir::Value cond, mlir::Block *trueTarget,
@@ -1467,9 +1467,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     assert(falseTarget && "missing conditional branch false block");
     mlir::Location loc = toLocation();
     mlir::Value bcc = builder->createConvert(loc, builder->getI1Type(), cond);
-    builder->create<mlir::cf::CondBranchOp>(loc, bcc, trueTarget,
-                                            mlir::ValueRange{}, falseTarget,
-                                            mlir::ValueRange{});
+    mlir::cf::CondBranchOp::create(*builder, loc, bcc, trueTarget,
+                                   mlir::ValueRange{}, falseTarget,
+                                   mlir::ValueRange{});
   }
   void genConditionalBranch(mlir::Value cond,
                             Fortran::lower::pft::Evaluation *trueTarget,
@@ -1637,28 +1637,28 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     assert((inArithmeticIfContext || !realSelector) && "invalid selector type");
     mlir::Value zero;
     if (inArithmeticIfContext)
-      zero =
-          realSelector
-              ? builder->create<mlir::arith::ConstantOp>(
-                    loc, selectorType, builder->getFloatAttr(selectorType, 0.0))
-              : builder->createIntegerConstant(loc, selectorType, 0);
+      zero = realSelector
+                 ? mlir::arith::ConstantOp::create(
+                       *builder, loc, selectorType,
+                       builder->getFloatAttr(selectorType, 0.0))
+                 : builder->createIntegerConstant(loc, selectorType, 0);
     for (auto label : llvm::enumerate(labelList)) {
       mlir::Value cond;
       if (realSelector) // inArithmeticIfContext
-        cond = builder->create<mlir::arith::CmpFOp>(
-            loc,
+        cond = mlir::arith::CmpFOp::create(
+            *builder, loc,
             label.index() == 0 ? mlir::arith::CmpFPredicate::OLT
                                : mlir::arith::CmpFPredicate::OGT,
             selector, zero);
       else if (inArithmeticIfContext) // INTEGER selector
-        cond = builder->create<mlir::arith::CmpIOp>(
-            loc,
+        cond = mlir::arith::CmpIOp::create(
+            *builder, loc,
             label.index() == 0 ? mlir::arith::CmpIPredicate::slt
                                : mlir::arith::CmpIPredicate::sgt,
             selector, zero);
       else // A value of 0 is an IO ERR branch: invert comparison.
-        cond = builder->create<mlir::arith::CmpIOp>(
-            loc,
+        cond = mlir::arith::CmpIOp::create(
+            *builder, loc,
             valueList[label.index()] == 0 ? mlir::arith::CmpIPredicate::ne
                                           : mlir::arith::CmpIPredicate::eq,
             selector,
@@ -1715,7 +1715,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     if (blockIsUnterminated()) {
       bridge.openAccCtx().finalizeAndKeep();
       bridge.fctCtx().finalizeAndKeep();
-      builder->create<mlir::func::ReturnOp>(toLocation(), retval);
+      mlir::func::ReturnOp::create(*builder, toLocation(), retval);
     }
     if (!earlyReturn) {
       bridge.openAccCtx().pop();
@@ -1795,7 +1795,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     if (mlir::Block *finalBlock = funit.finalBlock) {
       // The current block must end with a terminator.
       if (blockIsUnterminated())
-        builder->create<mlir::cf::BranchOp>(toLocation(), finalBlock);
+        mlir::cf::BranchOp::create(*builder, toLocation(), finalBlock);
       // Set insertion point to final block.
       builder->setInsertionPoint(finalBlock, finalBlock->end());
     }
@@ -1828,8 +1828,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     mlir::Value cond =
         builder->createConvert(loc, builder->getI1Type(), condExpr);
     if (negate)
-      cond = builder->create<mlir::arith::XOrIOp>(
-          loc, cond, builder->createIntegerConstant(loc, cond.getType(), 1));
+      cond = mlir::arith::XOrIOp::create(
+          *builder, loc, cond,
+          builder->createIntegerConstant(loc, cond.getType(), 1));
     return cond;
   }
 
@@ -1912,7 +1913,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
     stmtCtx.finalizeAndReset();
     // Raise an exception if REAL expr is a NaN.
     if (mlir::isa<mlir::FloatType>(expr.getType()))
-      expr = builder->create<mlir::arith::AddFOp>(toLocation(), expr, expr);
+      expr = mlir::arith::AddFOp::create(*builder, toLocation(), expr, expr);
     // An empty valueList indicates to genMultiwayBranch that the branch is
     // an ArithmeticIfStmt that has two branches on value 0 or 0.0.
     llvm::SmallVector<int64_t> valueList;
@@ -2517,27 +2518,28 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       mlir::Value tripCount;
       if (info.hasRealControl) {
         auto diff1 =
-            builder->create<mlir::arith::SubFOp>(loc, upperValue, lowerValue);
+            mlir::arith::SubFOp::create(*builder, loc, upperValue, lowerValue);
         auto diff2 =
-            builder->create<mlir::arith::AddFOp>(loc, diff1, stepValue);
-        tripCount = builder->create<mlir::arith::DivFOp>(loc, diff2, stepValue);
+            mlir::arith::AddFOp::create(*builder, loc, diff1, stepValue);
+        tripCount =
+            mlir::arith::DivFOp::create(*builder, loc, diff2, stepValue);
         tripCount =
             builder->createConvert(loc, builder->getIndexType(), tripCount);
       } else {
         auto diff1 =
-            builder->create<mlir::arith::SubIOp>(loc, upperValue, lowerValue);
+            mlir::arith::SubIOp::create(*builder, loc, upperValue, lowerValue);
         auto diff2 =
-            builder->create<mlir::arith::AddIOp>(loc, diff1, stepValue);
+            mlir::arith::AddIOp::create(*builder, loc, diff1, stepValue);
         tripCount =
-            builder->create<mlir::arith::DivSIOp>(loc, diff2, stepValue);
+            mlir::arith::DivSIOp::create(*builder, loc, diff2, stepValue);
       }
       if (forceLoopToExecuteOnce) { // minimum tripCount is 1
         mlir::Value one =
             builder->createIntegerConstant(loc, tripCount.getType(), 1);
-        auto cond = builder->create<mlir::arith::CmpIOp>(
-            loc, mlir::arith::CmpIPredicate::slt, tripCount, one);
+        auto cond = mlir::arith::CmpIOp::create(
+            *builder, loc, mlir::arith::CmpIPredicate::slt, tripCount, one);
         tripCount =
-            builder->create<mlir::arith::SelectOp>(loc, cond, one, tripCount);
+            mlir::arith::SelectOp::create(*builder, loc, cond, one, tripCount);
       }
       info.tripVariable = builder->createTemporary(loc, tripCount.getType());
       fir::StoreOp::create(*builder, loc, tripCount, info.tripVariable);
@@ -2549,8 +2551,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       tripCount = fir::LoadOp::create(*builder, loc, info.tripVariable);
       mlir::Value zero =
           builder->createIntegerConstant(loc, tripCount.getType(), 0);
-      auto cond = builder->create<mlir::arith::CmpIOp>(
-          loc, mlir::arith::CmpIPredicate::sgt, tripCount, zero);
+      auto cond = mlir::arith::CmpIOp::create(
+          *builder, loc, mlir::arith::CmpIPredicate::sgt, tripCount, zero);
       if (info.maskExpr) {
         genConditionalBranch(cond, info.maskBlock, info.exitBlock);
         startBlock(info.maskBlock);
@@ -2653,8 +2655,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         auto doLoopOp = mlir::cast<fir::DoLoopOp>(info.loopOp);
         builder->setInsertionPointToEnd(doLoopOp.getBody());
         llvm::SmallVector<mlir::Value, 2> results;
-        results.push_back(builder->create<mlir::arith::AddIOp>(
-            loc, doLoopOp.getInductionVar(), doLoopOp.getStep(), iofAttr));
+        results.push_back(mlir::arith::AddIOp::create(
+            *builder, loc, doLoopOp.getInductionVar(), doLoopOp.getStep(),
+            iofAttr));
         // Step loopVariable to help optimizations such as vectorization.
         // Induction variable elimination will clean up as necessary.
         mlir::Value step = builder->createConvert(
@@ -2662,7 +2665,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         mlir::Value loopVar =
             fir::LoadOp::create(*builder, loc, info.loopVariable);
         results.push_back(
-            builder->create<mlir::arith::AddIOp>(loc, loopVar, step, iofAttr));
+            mlir::arith::AddIOp::create(*builder, loc, loopVar, step, iofAttr));
         fir::ResultOp::create(*builder, loc, results);
         builder->setInsertionPointAfter(doLoopOp);
         // The loop control variable may be used after the loop.
@@ -2676,7 +2679,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
           fir::LoadOp::create(*builder, loc, info.tripVariable);
       mlir::Value one =
           builder->createIntegerConstant(loc, tripCount.getType(), 1);
-      tripCount = builder->create<mlir::arith::SubIOp>(loc, tripCount, one);
+      tripCount = mlir::arith::SubIOp::create(*builder, loc, tripCount, one);
       fir::StoreOp::create(*builder, loc, tripCount, info.tripVariable);
       mlir::Value value = fir::LoadOp::create(*builder, loc, info.loopVariable);
       mlir::Value step;
@@ -2685,9 +2688,10 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       else
         step = genControlValue(info.stepExpr, info);
       if (info.hasRealControl)
-        value = builder->create<mlir::arith::AddFOp>(loc, value, step);
+        value = mlir::arith::AddFOp::create(*builder, loc, value, step);
       else
-        value = builder->create<mlir::arith::AddIOp>(loc, value, step, iofAttr);
+        value =
+            mlir::arith::AddIOp::create(*builder, loc, value, step, iofAttr);
       fir::StoreOp::create(*builder, loc, value, info.loopVariable);
 
       genBranch(info.headerBlock);
@@ -3184,8 +3188,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       assert(funit && "not inside main program, function or subroutine");
       mlir::Block *continueBlock =
           builder->getBlock()->splitBlock(builder->getBlock()->end());
-      builder->create<mlir::cf::CondBranchOp>(toLocation(), exitCond,
-                                              funit->finalBlock, continueBlock);
+      mlir::cf::CondBranchOp::create(*builder, toLocation(), exitCond,
+                                     funit->finalBlock, continueBlock);
       builder->setInsertionPointToEnd(continueBlock);
     }
   }
@@ -3353,8 +3357,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
           step = fir::getBase(
               genExprValue(*Fortran::semantics::GetExpr(*expr), stmtCtx));
         else
-          step = builder->create<mlir::arith::ConstantIndexOp>(
-              loc, 1); // Use index type directly
+          step = mlir::arith::ConstantIndexOp::create(
+              *builder, loc, 1); // Use index type directly
 
         // Ensure lb, ub, and step are of index type using fir.convert
         lb = fir::ConvertOp::create(*builder, loc, idxTy, lb);
@@ -3625,7 +3629,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
       auto genCond = [&](mlir::Value rhs,
                          mlir::arith::CmpIPredicate pred) -> mlir::Value {
         if (!isCharSelector)
-          return builder->create<mlir::arith::CmpIOp>(loc, pred, selector, rhs);
+          return mlir::arith::CmpIOp::create(*builder, loc, pred, selector,
+                                             rhs);
         fir::factory::CharacterExprHelper charHe...
[truncated]

``````````

</details>


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


More information about the flang-commits mailing list