Skip to content

[CIR][ThroughMLIR] Support array type GlobalOp lowering with initial values #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/lib/CIR/Lowering/LoweringHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ convertStringAttrToDenseElementsAttr(mlir::cir::ConstArrayAttr attr,
assert(stringAttr && "expected string attribute here");
for (auto element : stringAttr)
values.push_back({8, (uint64_t)element});
auto arrayTy = mlir::dyn_cast<mlir::cir::ArrayType>(attr.getType());
assert(arrayTy && "String attribute must have an array type");
if (arrayTy.getSize() != stringAttr.size())
llvm_unreachable("array type of the length not equal to that of the string "
"attribute is not supported yet");
return mlir::DenseElementsAttr::get(
mlir::RankedTensorType::get({(int64_t)values.size()}, type),
llvm::ArrayRef(values));
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CIR/Lowering/ThroughMLIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_clang_library(clangCIRLoweringThroughMLIR
clangLex
clangFrontend
clangCIR
clangCIRLoweringHelpers
${dialect_libs}
MLIRCIR
MLIRAnalysis
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/LoweringHelpers.h"
#include "clang/CIR/LowerToMLIR.h"
#include "clang/CIR/Passes.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -936,7 +937,15 @@ class CIRGlobalOpLowering
mlir::Attribute initialValue = mlir::Attribute();
std::optional<mlir::Attribute> init = op.getInitialValue();
if (init.has_value()) {
if (auto constArr = mlir::dyn_cast<mlir::cir::ZeroAttr>(init.value())) {
if (auto constArr =
mlir::dyn_cast<mlir::cir::ConstArrayAttr>(init.value())) {
init = lowerConstArrayAttr(constArr, getTypeConverter());
if (init.has_value())
initialValue = init.value();
else
llvm_unreachable("GlobalOp lowering array with initial value fail");
} else if (auto constArr =
mlir::dyn_cast<mlir::cir::ZeroAttr>(init.value())) {
if (memrefType.getShape().size()) {
auto elementType = memrefType.getElementType();
auto rtt =
Expand Down
13 changes: 11 additions & 2 deletions clang/test/CIR/Lowering/ThroughMLIR/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// RUN: FileCheck --input-file=%t.mlir %s

float f[32000];
double d;

// CHECK: memref.global "public" @f : memref<32000xf32> = dense<0.000000e+00>
double d;
// CHECK: memref.global "public" @d : memref<f64> = dense<0.000000e+00>
float f_init[] = {1.0, 2.0};
// CHECK: memref.global "public" @f_init : memref<2xf32> = dense<[1.000000e+00, 2.000000e+00]>
int i_init[2] = {0, 1};
// CHECK: memref.global "public" @i_init : memref<2xi32> = dense<[0, 1]>
char string[] = "whatnow";
// CHECK: memref.global "public" @string : memref<8xi8> = dense<[119, 104, 97, 116, 110, 111, 119, 0]>
int excess_sint[4] = {1, 2};
// CHECK: memref.global "public" @excess_sint : memref<4xi32> = dense<[1, 2, 0, 0]>
int sint[] = {123, 456, 789};
// CHECK: memref.global "public" @sint : memref<3xi32> = dense<[123, 456, 789]>
Loading