[flang-commits] [flang] e0c782b - [flang] Add global and global box initialization tests
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Sun Apr 3 06:21:03 PDT 2022
Author: Valentin Clement
Date: 2022-04-03T15:20:55+02:00
New Revision: e0c782bdc0e9b9bb6ff69e224121adaba8aab843
URL: https://github.com/llvm/llvm-project/commit/e0c782bdc0e9b9bb6ff69e224121adaba8aab843
DIFF: https://github.com/llvm/llvm-project/commit/e0c782bdc0e9b9bb6ff69e224121adaba8aab843.diff
LOG: [flang] Add global and global box initialization tests
This patch addes some global initialization and global
box initialization tests.
This patch is part of the upstreaming effort from fir-dev branch.
Reviewed By: schweitz
Differential Revision: https://reviews.llvm.org/D122881
Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Added:
flang/test/Fir/boxchar.fir
flang/test/Fir/global-initialization.fir
flang/test/Fir/global.fir
Modified:
flang/lib/Optimizer/CodeGen/CodeGen.cpp
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index affed344577df..484edd9d30265 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -598,21 +598,30 @@ struct StringLitOpConversion : public FIROpConversion<fir::StringLitOp> {
return success();
}
- auto arr = attr.cast<mlir::ArrayAttr>();
auto charTy = constop.getType().cast<fir::CharacterType>();
unsigned bits = lowerTy().characterBitsize(charTy);
mlir::Type intTy = rewriter.getIntegerType(bits);
- auto attrs = llvm::map_range(
- arr.getValue(), [intTy, bits](mlir::Attribute attr) -> Attribute {
- return mlir::IntegerAttr::get(
- intTy,
- attr.cast<mlir::IntegerAttr>().getValue().sextOrTrunc(bits));
- });
- mlir::Type vecType = mlir::VectorType::get(arr.size(), intTy);
- auto denseAttr = mlir::DenseElementsAttr::get(
- vecType.cast<mlir::ShapedType>(), llvm::to_vector<8>(attrs));
- rewriter.replaceOpWithNewOp<mlir::arith::ConstantOp>(constop, ty,
- denseAttr);
+ mlir::Location loc = constop.getLoc();
+ mlir::Value cst = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
+ if (auto arr = attr.dyn_cast<mlir::DenseElementsAttr>()) {
+ cst = rewriter.create<mlir::LLVM::ConstantOp>(loc, ty, arr);
+ } else if (auto arr = attr.dyn_cast<mlir::ArrayAttr>()) {
+ for (auto a : llvm::enumerate(arr.getValue())) {
+ // convert each character to a precise bitsize
+ auto elemAttr = mlir::IntegerAttr::get(
+ intTy,
+ a.value().cast<mlir::IntegerAttr>().getValue().zextOrTrunc(bits));
+ auto elemCst =
+ rewriter.create<mlir::LLVM::ConstantOp>(loc, intTy, elemAttr);
+ auto index = mlir::ArrayAttr::get(
+ constop.getContext(), rewriter.getI32IntegerAttr(a.index()));
+ cst = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, cst, elemCst,
+ index);
+ }
+ } else {
+ return failure();
+ }
+ rewriter.replaceOp(constop, cst);
return success();
}
};
diff --git a/flang/test/Fir/boxchar.fir b/flang/test/Fir/boxchar.fir
new file mode 100644
index 0000000000000..536d59d285449
--- /dev/null
+++ b/flang/test/Fir/boxchar.fir
@@ -0,0 +1,22 @@
+// RUN: tco --target=x86_64-unknown-linux-gnu %s | FileCheck %s
+
+// Test of building and passing boxchar.
+
+func private @callee(%x : !fir.boxchar<1>)
+
+// CHECK-LABEL: define void @get_name
+func @get_name() {
+ %1 = fir.address_of (@name) : !fir.ref<!fir.char<1,9>>
+ %2 = arith.constant 9 : i64
+ %3 = fir.convert %1 : (!fir.ref<!fir.char<1,9>>) -> !fir.ref<!fir.char<1,?>>
+ %4 = fir.emboxchar %3, %2 : (!fir.ref<!fir.char<1,?>>, i64) -> !fir.boxchar<1>
+ // CHECK: call void @callee(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @name, i32 0, i32 0), i64 9)
+ fir.call @callee(%4) : (!fir.boxchar<1>) -> ()
+ return
+}
+
+fir.global @name constant : !fir.char<1,9> {
+ %str = fir.string_lit "Your name"(9) : !fir.char<1,9>
+ //constant 1
+ fir.has_value %str : !fir.char<1,9>
+}
diff --git a/flang/test/Fir/global-initialization.fir b/flang/test/Fir/global-initialization.fir
new file mode 100644
index 0000000000000..638a39759ca90
--- /dev/null
+++ b/flang/test/Fir/global-initialization.fir
@@ -0,0 +1,69 @@
+// RUN: tco --fir-to-llvm-ir %s | FileCheck %s
+
+fir.global internal @_QEmask : !fir.array<32xi32> {
+ %c0_i32 = arith.constant 1 : i32
+ %0 = fir.undefined !fir.array<32xi32>
+ %2 = fir.insert_on_range %0, %c0_i32 from (0) to (31) : (!fir.array<32xi32>, i32) -> !fir.array<32xi32>
+ fir.has_value %2 : !fir.array<32xi32>
+}
+
+// CHECK: llvm.mlir.global internal @_QEmask() : !llvm.array<32 x i32> {
+// CHECK: [[VAL0:%.*]] = llvm.mlir.constant(1 : i32) : i32
+// CHECK: [[VAL1:%.*]] = llvm.mlir.undef : !llvm.array<32 x i32>
+// CHECK: [[VAL2:%.*]] = llvm.mlir.constant(dense<1> : vector<32xi32>) : !llvm.array<32 x i32>
+// CHECK: llvm.return [[VAL2]] : !llvm.array<32 x i32>
+// CHECK: }
+
+fir.global internal @_QEmultiarray : !fir.array<32x32xi32> {
+ %c0_i32 = arith.constant 1 : i32
+ %0 = fir.undefined !fir.array<32x32xi32>
+ %2 = fir.insert_on_range %0, %c0_i32 from (0,0) to (31,31) : (!fir.array<32x32xi32>, i32) -> !fir.array<32x32xi32>
+ fir.has_value %2 : !fir.array<32x32xi32>
+}
+
+// CHECK: llvm.mlir.global internal @_QEmultiarray() : !llvm.array<32 x array<32 x i32>> {
+// CHECK: [[VAL0:%.*]] = llvm.mlir.constant(1 : i32) : i32
+// CHECK: [[VAL1:%.*]] = llvm.mlir.undef : !llvm.array<32 x array<32 x i32>>
+// CHECK: [[VAL2:%.*]] = llvm.mlir.constant(dense<1> : vector<32x32xi32>) : !llvm.array<32 x array<32 x i32>>
+// CHECK: llvm.return [[VAL2]] : !llvm.array<32 x array<32 x i32>>
+// CHECK: }
+
+fir.global internal @_QEmasklogical : !fir.array<32768x!fir.logical<4>> {
+ %true = arith.constant true
+ %0 = fir.undefined !fir.array<32768x!fir.logical<4>>
+ %1 = fir.convert %true : (i1) -> !fir.logical<4>
+ %2 = fir.insert_on_range %0, %1 from (0) to (32767) : (!fir.array<32768x!fir.logical<4>>, !fir.logical<4>) -> !fir.array<32768x!fir.logical<4>>
+ fir.has_value %2 : !fir.array<32768x!fir.logical<4>>
+}
+
+// CHECK: llvm.mlir.global internal @_QEmasklogical() : !llvm.array<32768 x i32> {
+// CHECK: [[VAL0:%.*]] = llvm.mlir.constant(true) : i1
+// CHECK: [[VAL1:%.*]] = llvm.mlir.undef : !llvm.array<32768 x i32>
+// CHECK: [[VAL2:%.*]] = llvm.zext [[VAL0]] : i1 to i32
+// CHECK: [[VAL3:%.*]] = llvm.mlir.constant(dense<true> : vector<32768xi1>) : !llvm.array<32768 x i32>
+// CHECK: llvm.return [[VAL3]] : !llvm.array<32768 x i32>
+// CHECK: }
+
+fir.global internal @_QElookforme : !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}> {
+ %c2_i32 = arith.constant 2 : i32
+ %c52_i32 = arith.constant 52 : i32
+ %0 = fir.undefined !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>
+ %1 = fir.undefined !fir.array<500xi32>
+ %2 = fir.insert_on_range %1, %c2_i32 from (0) to (499) : (!fir.array<500xi32>, i32) -> !fir.array<500xi32>
+ %3 = fir.insert_value %0, %2, ["i", !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>] :(!fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>, !fir.array<500xi32>) -> !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>
+ %4 = fir.insert_on_range %1, %c52_i32 from (0) to (499) : (!fir.array<500xi32>, i32) -> !fir.array<500xi32>
+ %5 = fir.insert_value %3, %4, ["j", !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>] : (!fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>, !fir.array<500xi32>) -> !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>
+ fir.has_value %5 : !fir.type<_QTt{i:!fir.array<500xi32>,j:!fir.array<500xi32>}>
+}
+
+// CHECK: llvm.mlir.global internal @_QElookforme() : !llvm.struct<"_QTt", (array<500 x i32>, array<500 x i32>)> {
+// CHECK: [[CST0:%.*]] = llvm.mlir.constant(2 : i32) : i32
+// CHECK: [[CST1:%.*]] = llvm.mlir.constant(52 : i32) : i32
+// CHECK: [[STRUCT:%.*]] = llvm.mlir.undef : !llvm.struct<"_QTt", (array<500 x i32>, array<500 x i32>)>
+// CHECK: [[ARR1:%.*]] = llvm.mlir.undef : !llvm.array<500 x i32>
+// CHECK: [[DENSE1:%.*]] = llvm.mlir.constant(dense<2> : vector<500xi32>) : !llvm.array<500 x i32>
+// CHECK: [[STRUCT1:%.*]] = llvm.insertvalue [[DENSE1]], [[STRUCT]][0 : i32] : !llvm.struct<"_QTt", (array<500 x i32>, array<500 x i32>)>
+// CHECK: [[DENSE2:%.*]] = llvm.mlir.constant(dense<52> : vector<500xi32>) : !llvm.array<500 x i32>
+// CHECK: [[STRUCT2:%.*]] = llvm.insertvalue [[DENSE2]], [[STRUCT1]][1 : i32] : !llvm.struct<"_QTt", (array<500 x i32>, array<500 x i32>)>
+// CHECK: llvm.return [[STRUCT2]] : !llvm.struct<"_QTt", (array<500 x i32>, array<500 x i32>)>
+// CHECK: }
diff --git a/flang/test/Fir/global.fir b/flang/test/Fir/global.fir
new file mode 100644
index 0000000000000..a29c1da4974af
--- /dev/null
+++ b/flang/test/Fir/global.fir
@@ -0,0 +1,59 @@
+// RUN: tco %s | FileCheck %s
+
+// CHECK: @g_i0 = global i32 0
+fir.global @g_i0 : i32 {
+ %1 = arith.constant 0 : i32
+ fir.has_value %1 : i32
+}
+
+// CHECK: @g_i2 = global i32 2
+fir.global @g_i2 : i32 {
+ %1 = arith.constant 2 : i32
+ fir.has_value %1 : i32
+}
+
+// CHECK: @g_ci5 = constant i32 5
+fir.global @g_ci5 constant : i32 {
+ %c = arith.constant 5 : i32
+ fir.has_value %c : i32
+}
+
+// CHECK: @i_i515 = internal global i32 515
+fir.global internal @i_i515 (515:i32) : i32
+
+// CHECK: @C_i511 = common global i32 0
+fir.global common @C_i511 (0:i32) : i32
+
+// CHECK: @w_i86 = weak global i32 86
+fir.global weak @w_i86 (86:i32) : i32
+
+// CHECK: @str1 = global [6 x i8] c"Hello!"
+fir.global @str1 : !fir.char<1,6> {
+ %1 = fir.string_lit "Hello!"(6) : !fir.char<1,6>
+ fir.has_value %1 : !fir.char<1,6>
+}
+
+// CHECK: @_QEmask = internal global [32 x i32] [i32 1, i32 1
+fir.global internal @_QEmask : !fir.array<32xi32> {
+ %c0_i32 = arith.constant 1 : i32
+ %0 = fir.undefined !fir.array<32xi32>
+ %2 = fir.insert_on_range %0, %c0_i32 from (0) to (31) : (!fir.array<32xi32>, i32) -> !fir.array<32xi32>
+ fir.has_value %2 : !fir.array<32xi32>
+}
+
+// CHECK: @_QEmultiarray = internal global [32 x [32 x i32]]
+fir.global internal @_QEmultiarray : !fir.array<32x32xi32> {
+ %c0_i32 = arith.constant 1 : i32
+ %0 = fir.undefined !fir.array<32x32xi32>
+ %2 = fir.insert_on_range %0, %c0_i32 from (0,0) to (31,31) : (!fir.array<32x32xi32>, i32) -> !fir.array<32x32xi32>
+ fir.has_value %2 : !fir.array<32x32xi32>
+}
+
+// CHECK: @_QEmasklogical = internal global [32768 x i32] [i32 -1, i32 -1,
+fir.global internal @_QEmasklogical : !fir.array<32768x!fir.logical<4>> {
+ %true = arith.constant true
+ %0 = fir.undefined !fir.array<32768x!fir.logical<4>>
+ %1 = fir.convert %true : (i1) -> !fir.logical<4>
+ %2 = fir.insert_on_range %0, %1 from (0) to (32767) : (!fir.array<32768x!fir.logical<4>>, !fir.logical<4>) -> !fir.array<32768x!fir.logical<4>>
+ fir.has_value %2 : !fir.array<32768x!fir.logical<4>>
+}
More information about the flang-commits
mailing list