[flang-commits] [flang] [flang] Add fir.declare handling in --fir-to-llvm-ir (PR #93487)

via flang-commits flang-commits at lists.llvm.org
Mon May 27 10:27:06 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-codegen

Author: Kelvin Li (kkwli)

<details>
<summary>Changes</summary>


```
func.func @<!-- -->_QPsub() {
  %0 = fir.alloca i32 {bindc_name = "x", uniq_name = "_QFsubEx"}
  %1 = fir.declare %0 {uniq_name = "_QFsubEx"} : (!fir.ref<i32>) -> !fir.ref<i32>
  return
}
```
Currently, `fir.declare` is not recognized in `--fir-to-llvm-ir`.
```
$ fir-opt --fir-to-llvm-ir declare.mlir 
declare.mlir:3:10: error: failed to legalize operation 'fir.declare'
    %1 = fir.declare %0 {uniq_name = "_QFsubEx"} : (!fir.ref<i32>) -> !fir.ref<i32>
         ^
declare.mlir:3:10: note: see current operation: %3 = "fir.declare"(%2) <{operandSegmentSizes = array<i32: 1, 0, 0, 0>, uniq_name = "_QFsubEx"}> : (!fir.ref<i32>) -> !fir.ref<i32>
```


---
Full diff: https://github.com/llvm/llvm-project/pull/93487.diff


1 Files Affected:

- (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+20-17) 


``````````diff
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 74e68725003cb..e40ba6f734191 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -171,11 +171,12 @@ genAllocationScaleSize(OP op, mlir::Type ity,
 }
 
 namespace {
-struct DeclareOpConversion : public fir::FIROpConversion<fir::cg::XDeclareOp> {
-public:
-  using FIROpConversion::FIROpConversion;
+template <typename OP>
+struct DeclareCommonConversion : public fir::FIROpConversion<OP> {
+  using fir::FIROpConversion<OP>::FIROpConversion;
+  using OpAdaptor = typename OP::Adaptor;
   mlir::LogicalResult
-  matchAndRewrite(fir::cg::XDeclareOp declareOp, OpAdaptor adaptor,
+  matchAndRewrite(OP declareOp, OpAdaptor adaptor,
                   mlir::ConversionPatternRewriter &rewriter) const override {
     auto memRef = adaptor.getOperands()[0];
     if (auto fusedLoc = mlir::dyn_cast<mlir::FusedLoc>(declareOp.getLoc())) {
@@ -3752,19 +3753,21 @@ void fir::populateFIRToLLVMConversionPatterns(
       BoxOffsetOpConversion, BoxProcHostOpConversion, BoxRankOpConversion,
       BoxTypeCodeOpConversion, BoxTypeDescOpConversion, CallOpConversion,
       CmpcOpConversion, ConstcOpConversion, ConvertOpConversion,
-      CoordinateOpConversion, DTEntryOpConversion, DeclareOpConversion,
-      DivcOpConversion, EmboxOpConversion, EmboxCharOpConversion,
-      EmboxProcOpConversion, ExtractValueOpConversion, FieldIndexOpConversion,
-      FirEndOpConversion, FreeMemOpConversion, GlobalLenOpConversion,
-      GlobalOpConversion, HasValueOpConversion, InsertOnRangeOpConversion,
-      InsertValueOpConversion, IsPresentOpConversion, LenParamIndexOpConversion,
-      LoadOpConversion, MulcOpConversion, NegcOpConversion,
-      NoReassocOpConversion, SelectCaseOpConversion, SelectOpConversion,
-      SelectRankOpConversion, SelectTypeOpConversion, ShapeOpConversion,
-      ShapeShiftOpConversion, ShiftOpConversion, SliceOpConversion,
-      StoreOpConversion, StringLitOpConversion, SubcOpConversion,
-      TypeDescOpConversion, TypeInfoOpConversion, UnboxCharOpConversion,
-      UnboxProcOpConversion, UndefOpConversion, UnreachableOpConversion,
+      CoordinateOpConversion, DTEntryOpConversion,
+      DeclareCommonConversion<fir::cg::XDeclareOp>,
+      DeclareCommonConversion<fir::DeclareOp>, DivcOpConversion,
+      EmboxOpConversion, EmboxCharOpConversion, EmboxProcOpConversion,
+      ExtractValueOpConversion, FieldIndexOpConversion, FirEndOpConversion,
+      FreeMemOpConversion, GlobalLenOpConversion, GlobalOpConversion,
+      HasValueOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion,
+      IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
+      MulcOpConversion, NegcOpConversion, NoReassocOpConversion,
+      SelectCaseOpConversion, SelectOpConversion, SelectRankOpConversion,
+      SelectTypeOpConversion, ShapeOpConversion, ShapeShiftOpConversion,
+      ShiftOpConversion, SliceOpConversion, StoreOpConversion,
+      StringLitOpConversion, SubcOpConversion, TypeDescOpConversion,
+      TypeInfoOpConversion, UnboxCharOpConversion, UnboxProcOpConversion,
+      UndefOpConversion, UnreachableOpConversion,
       UnrealizedConversionCastOpConversion, XArrayCoorOpConversion,
       XEmboxOpConversion, XReboxOpConversion, ZeroOpConversion>(converter,
                                                                 options);

``````````

</details>


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


More information about the flang-commits mailing list