[flang-commits] [flang] 49ee563 - [flang] Add fir.box_typecode operation
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Mon Nov 14 01:51:24 PST 2022
Author: Valentin Clement
Date: 2022-11-14T10:51:18+01:00
New Revision: 49ee5634e7b00991a1315535eae4bf996e84c387
URL: https://github.com/llvm/llvm-project/commit/49ee5634e7b00991a1315535eae4bf996e84c387
DIFF: https://github.com/llvm/llvm-project/commit/49ee5634e7b00991a1315535eae4bf996e84c387.diff
LOG: [flang] Add fir.box_typecode operation
`fir.box_typecode` operation allows to retrieve the type code
from a boxed value. This will be used in the `fir.select_type` conversion
to if-then-else ladder for type guard statement with intrinsic type spec
instead of using a runtime call.
Reviewed By: jeanPerier
Differential Revision: https://reviews.llvm.org/D137829
Added:
flang/test/Fir/box-typecode.fir
Modified:
flang/include/flang/Optimizer/Dialect/FIROps.td
flang/lib/Optimizer/CodeGen/CodeGen.cpp
flang/test/Fir/fir-ops.fir
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index 005fad8362428..1a6493e2c8fbb 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -1053,6 +1053,23 @@ def fir_BoxEleSizeOp : fir_SimpleOneResultOp<"box_elesize", [NoMemoryEffect]> {
let results = (outs AnyIntegerLike);
}
+def fir_BoxTypeCodeOp : fir_SimpleOneResultOp<"box_typecode", [NoMemoryEffect]>
+{
+ let summary = "return the type code the boxed value";
+
+ let description = [{
+ Returns the descriptor type code of an entity of `box` type.
+
+ ```mlir
+ %1 = fir.box_type %0 : (!fir.box<T>) -> i32
+ ```
+ }];
+
+ let arguments = (ins BoxOrClassType:$box);
+
+ let results = (outs AnyIntegerLike);
+}
+
def fir_BoxIsAllocOp : fir_SimpleOp<"box_isalloc", [NoMemoryEffect]> {
let summary = "is the boxed value an ALLOCATABLE?";
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 81b55422087de..f8a46a9d9555e 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -650,6 +650,23 @@ struct BoxTypeDescOpConversion : public FIROpConversion<fir::BoxTypeDescOp> {
}
};
+/// Lower `fir.box_typecode` to a sequence of operations to extract the type
+/// code in the boxed value.
+struct BoxTypeCodeOpConversion : public FIROpConversion<fir::BoxTypeCodeOp> {
+ using FIROpConversion::FIROpConversion;
+
+ mlir::LogicalResult
+ matchAndRewrite(fir::BoxTypeCodeOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ mlir::Value box = adaptor.getOperands()[0];
+ auto loc = box.getLoc();
+ auto ty = convertType(op.getType());
+ auto typeCode = getValueFromBox(loc, box, ty, rewriter, kTypePosInBox);
+ rewriter.replaceOp(op, typeCode);
+ return mlir::success();
+ }
+};
+
/// Lower `fir.string_lit` to LLVM IR dialect operation.
struct StringLitOpConversion : public FIROpConversion<fir::StringLitOp> {
using FIROpConversion::FIROpConversion;
@@ -3584,24 +3601,24 @@ class FIRToLLVMLowering
AllocaOpConversion, AllocMemOpConversion, BoxAddrOpConversion,
BoxCharLenOpConversion, BoxDimsOpConversion, BoxEleSizeOpConversion,
BoxIsAllocOpConversion, BoxIsArrayOpConversion, BoxIsPtrOpConversion,
- BoxProcHostOpConversion, BoxRankOpConversion, BoxTypeDescOpConversion,
- CallOpConversion, CmpcOpConversion, ConstcOpConversion,
- ConvertOpConversion, CoordinateOpConversion, DispatchOpConversion,
- DispatchTableOpConversion, DTEntryOpConversion, DivcOpConversion,
- EmboxOpConversion, EmboxCharOpConversion, EmboxProcOpConversion,
- ExtractValueOpConversion, FieldIndexOpConversion, FirEndOpConversion,
- FreeMemOpConversion, GenTypeDescOpConversion, GlobalLenOpConversion,
- GlobalOpConversion, HasValueOpConversion, InsertOnRangeOpConversion,
- InsertValueOpConversion, IsPresentOpConversion,
- LenParamIndexOpConversion, LoadOpConversion, MulcOpConversion,
- NegcOpConversion, NoReassocOpConversion, SelectCaseOpConversion,
- SelectOpConversion, SelectRankOpConversion, SelectTypeOpConversion,
- ShapeOpConversion, ShapeShiftOpConversion, ShiftOpConversion,
- SliceOpConversion, StoreOpConversion, StringLitOpConversion,
- SubcOpConversion, UnboxCharOpConversion, UnboxProcOpConversion,
- UndefOpConversion, UnreachableOpConversion, XArrayCoorOpConversion,
- XEmboxOpConversion, XReboxOpConversion, ZeroOpConversion>(
- typeConverter, options, bindingTables);
+ BoxProcHostOpConversion, BoxRankOpConversion, BoxTypeCodeOpConversion,
+ BoxTypeDescOpConversion, CallOpConversion, CmpcOpConversion,
+ ConstcOpConversion, ConvertOpConversion, CoordinateOpConversion,
+ DispatchOpConversion, DispatchTableOpConversion, DTEntryOpConversion,
+ DivcOpConversion, EmboxOpConversion, EmboxCharOpConversion,
+ EmboxProcOpConversion, ExtractValueOpConversion, FieldIndexOpConversion,
+ FirEndOpConversion, FreeMemOpConversion, GenTypeDescOpConversion,
+ GlobalLenOpConversion, GlobalOpConversion, HasValueOpConversion,
+ InsertOnRangeOpConversion, InsertValueOpConversion,
+ IsPresentOpConversion, LenParamIndexOpConversion, LoadOpConversion,
+ MulcOpConversion, NegcOpConversion, NoReassocOpConversion,
+ SelectCaseOpConversion, SelectOpConversion, SelectRankOpConversion,
+ SelectTypeOpConversion, ShapeOpConversion, ShapeShiftOpConversion,
+ ShiftOpConversion, SliceOpConversion, StoreOpConversion,
+ StringLitOpConversion, SubcOpConversion, UnboxCharOpConversion,
+ UnboxProcOpConversion, UndefOpConversion, UnreachableOpConversion,
+ XArrayCoorOpConversion, XEmboxOpConversion, XReboxOpConversion,
+ ZeroOpConversion>(typeConverter, options, bindingTables);
mlir::populateFuncToLLVMConversionPatterns(typeConverter, pattern);
mlir::populateOpenMPToLLVMConversionPatterns(typeConverter, pattern);
mlir::arith::populateArithToLLVMConversionPatterns(typeConverter, pattern);
diff --git a/flang/test/Fir/box-typecode.fir b/flang/test/Fir/box-typecode.fir
new file mode 100644
index 0000000000000..51b2d027a5d43
--- /dev/null
+++ b/flang/test/Fir/box-typecode.fir
@@ -0,0 +1,12 @@
+// RUN: tco %s | FileCheck %s
+
+func.func @test_box_typecode(%a: !fir.class<none>) -> i32 {
+ %0 = fir.box_typecode %a : (!fir.class<none>) -> i32
+ return %0 : i32
+}
+
+// CHECK-LABEL: @test_box_typecode(
+// CHECK-SAME: ptr %[[BOX:.*]])
+// CHECK: %[[GEP:.*]] = getelementptr { ptr, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}}, i{{.*}} }, ptr %[[BOX]], i32 0, i32 4
+// CHECK: %[[TYPE_CODE:.*]] = load i32, ptr %[[GEP]]
+// CHECK: ret i32 %[[TYPE_CODE]]
diff --git a/flang/test/Fir/fir-ops.fir b/flang/test/Fir/fir-ops.fir
index 486c7ee809910..323c2b590dc6e 100644
--- a/flang/test/Fir/fir-ops.fir
+++ b/flang/test/Fir/fir-ops.fir
@@ -882,3 +882,12 @@ func.func @test_fortran_var_attrs() {
// CHECK: fir.alloca f32 {fortran_attrs = #fir.var_attrs<volatile>}
// CHECK: fir.alloca !fir.box<!fir.ptr<!fir.array<?xf32>>> {fortran_attrs = #fir.var_attrs<contiguous, pointer, volatile>}
}
+
+func.func @test_box_typecode(%a: !fir.class<none>) {
+ %0 = fir.box_typecode %a : (!fir.class<none>) -> i32
+ return
+}
+
+// CHECK-LABEL: func.func @test_box_typecode(
+// CHECK-SAME: %[[A:.*]]: !fir.class<none>)
+// CHECK: %{{.*}} = fir.box_typecode %[[A]] : (!fir.class<none>) -> i32
More information about the flang-commits
mailing list