Skip to content

Commit e136285

Browse files
authored
[CIR][ThroughMLIR] Support array type GlobalOp lowering with initial values (#753)
This commit makes the changes as following. 1. Enable array type GlobalOp lowering with initial values 2. Add error message when array size is not equal to initial string value size E.g. char big_string[10] = "abc";
1 parent 53f9d44 commit e136285

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

clang/lib/CIR/Lowering/LoweringHelpers.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ convertStringAttrToDenseElementsAttr(mlir::cir::ConstArrayAttr attr,
1919
assert(stringAttr && "expected string attribute here");
2020
for (auto element : stringAttr)
2121
values.push_back({8, (uint64_t)element});
22+
auto arrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType());
23+
assert(arrayTy && "String attribute must have an array type");
24+
if (arrayTy.getSize() != stringAttr.size())
25+
llvm_unreachable("array type of the length not equal to that of the string "
26+
"attribute is not supported yet");
2227
return mlir::DenseElementsAttr::get(
2328
mlir::RankedTensorType::get({(int64_t)values.size()}, type),
2429
llvm::ArrayRef(values));

clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_clang_library(clangCIRLoweringThroughMLIR
2727
clangLex
2828
clangFrontend
2929
clangCIR
30+
clangCIRLoweringHelpers
3031
${dialect_libs}
3132
MLIRCIR
3233
MLIRAnalysis

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "mlir/Transforms/DialectConversion.h"
4747
#include "clang/CIR/Dialect/IR/CIRDialect.h"
4848
#include "clang/CIR/Dialect/IR/CIRTypes.h"
49+
#include "clang/CIR/LoweringHelpers.h"
4950
#include "clang/CIR/LowerToMLIR.h"
5051
#include "clang/CIR/Passes.h"
5152
#include "llvm/ADT/STLExtras.h"
@@ -936,7 +937,15 @@ class CIRGlobalOpLowering
936937
mlir::Attribute initialValue = mlir::Attribute();
937938
std::optional<mlir::Attribute> init = op.getInitialValue();
938939
if (init.has_value()) {
939-
if (auto constArr = mlir::dyn_cast<mlir::cir::ZeroAttr>(init.value())) {
940+
if (auto constArr =
941+
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(init.value())) {
942+
init = lowerConstArrayAttr(constArr, getTypeConverter());
943+
if (init.has_value())
944+
initialValue = init.value();
945+
else
946+
llvm_unreachable("GlobalOp lowering array with initial value fail");
947+
} else if (auto constArr =
948+
mlir::dyn_cast<mlir::cir::ZeroAttr>(init.value())) {
940949
if (memrefType.getShape().size()) {
941950
auto elementType = memrefType.getElementType();
942951
auto rtt =

clang/test/CIR/Lowering/ThroughMLIR/global.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
// RUN: FileCheck --input-file=%t.mlir %s
33

44
float f[32000];
5-
double d;
6-
75
// CHECK: memref.global "public" @f : memref<32000xf32> = dense<0.000000e+00>
6+
double d;
87
// CHECK: memref.global "public" @d : memref<f64> = dense<0.000000e+00>
8+
float f_init[] = {1.0, 2.0};
9+
// CHECK: memref.global "public" @f_init : memref<2xf32> = dense<[1.000000e+00, 2.000000e+00]>
10+
int i_init[2] = {0, 1};
11+
// CHECK: memref.global "public" @i_init : memref<2xi32> = dense<[0, 1]>
12+
char string[] = "whatnow";
13+
// CHECK: memref.global "public" @string : memref<8xi8> = dense<[119, 104, 97, 116, 110, 111, 119, 0]>
14+
int excess_sint[4] = {1, 2};
15+
// CHECK: memref.global "public" @excess_sint : memref<4xi32> = dense<[1, 2, 0, 0]>
16+
int sint[] = {123, 456, 789};
17+
// CHECK: memref.global "public" @sint : memref<3xi32> = dense<[123, 456, 789]>

0 commit comments

Comments
 (0)