Skip to content
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

[CIR][CIRGen] Improve emission for array of unions #1236

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExprConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,20 @@ class ConstExprEmitter
return {};
}

auto desiredType = CGM.getTypes().ConvertType(T);
bool isDesiredArrayOfUnionDirectly = [&]() {
ChuanqiXu9 marked this conversation as resolved.
Show resolved Hide resolved
auto desiredArrayType = dyn_cast<cir::ArrayType>(desiredType);
if (!desiredArrayType)
return false;

auto elementStructType =
dyn_cast<cir::StructType>(desiredArrayType.getEltType());
if (!elementStructType)
return false;

return elementStructType.isUnion();
}();

// Emit initializer elements as MLIR attributes and check for common type.
mlir::Type CommonElementType;
for (unsigned i = 0; i != NumInitableElts; ++i) {
Expand All @@ -1024,10 +1038,12 @@ class ConstExprEmitter
return {};
if (i == 0)
CommonElementType = C.getType();
else if (isDesiredArrayOfUnionDirectly &&
C.getType() != CommonElementType)
CommonElementType = nullptr;
Elts.push_back(std::move(C));
}

auto desiredType = CGM.getTypes().ConvertType(T);
auto typedFiller = llvm::dyn_cast_or_null<mlir::TypedAttr>(Filler);
if (Filler && !typedFiller)
llvm_unreachable("We shouldn't be receiving untyped attrs here");
Expand Down
19 changes: 19 additions & 0 deletions clang/test/CIR/Lowering/union-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -fno-clangir-call-conv-lowering %s -o %t.ll
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
ChuanqiXu9 marked this conversation as resolved.
Show resolved Hide resolved

typedef struct {
char a;
} S_1;

typedef struct {
long a, b;
} S_2;

typedef union {
S_1 a;
S_2 b;
} U;

void foo() { U arr[2] = {{.b = {1, 2}}, {.a = {1}}}; }

// LLVM: store { { %struct.S_2 }, { %struct.S_1, [15 x i8] } } { { %struct.S_2 } { %struct.S_2 { i64 1, i64 2 } }, { %struct.S_1, [15 x i8] } { %struct.S_1 { i8 1 }, [15 x i8] zeroinitializer } }
Loading