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]Use unique ID to distinguish same name structure. #740

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ CIRGenTypes::~CIRGenTypes() {
// This is CIR's version of CIRGenTypes::addRecordTypeName
std::string CIRGenTypes::getRecordTypeName(const clang::RecordDecl *recordDecl,
StringRef suffix) {

const auto *key = Context.getTagDeclType(recordDecl).getTypePtr();
if (nameOfRecordDeclTypes.find(key) != nameOfRecordDeclTypes.end()) {
return nameOfRecordDeclTypes[key];
};

llvm::SmallString<256> typeName;
llvm::raw_svector_ostream outStream(typeName);

Expand Down Expand Up @@ -89,7 +95,18 @@ std::string CIRGenTypes::getRecordTypeName(const clang::RecordDecl *recordDecl,
if (!suffix.empty())
outStream << suffix;

return std::string(typeName);
if (nameUniqueIDOfRDtypes.find(typeName) != nameUniqueIDOfRDtypes.end()) {
unsigned int nameUniqueID = (++nameUniqueIDOfRDtypes[typeName]);

outStream << ".";
outStream << std::to_string(nameUniqueID);
} else {
nameUniqueIDOfRDtypes[typeName] = 0;
}

nameOfRecordDeclTypes[key] = std::string(typeName);

return nameOfRecordDeclTypes[key];
}

/// Return true if the specified type is already completely laid out.
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ class CIRGenTypes {
/// Contains the CIR type for any converted RecordDecl
llvm::DenseMap<const clang::Type *, mlir::cir::StructType> recordDeclTypes;

/// Contains the final name for any converted RecordDecl
llvm::DenseMap<const clang::Type *, std::string> nameOfRecordDeclTypes;
llvm::StringMap<unsigned int> nameUniqueIDOfRDtypes;

/// Hold memoized CIRGenFunctionInfo results
llvm::FoldingSet<CIRGenFunctionInfo> FunctionInfos;

Expand Down
34 changes: 34 additions & 0 deletions clang/test/CIR/CodeGen/same-name-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s

typedef struct demo{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to build the testcase without your PR, the crash doesn't seem much related with fixing the unique names, what am I missing?

int memberOne;
} demo;

void testOne() {
demo one = {1};
}

void testTwo() {
typedef struct demo {
int memberOne;
int memberTwo;
} demo;

demo two = {1, 2};
}

// CHECK: !ty_22demo22 = !cir.struct<struct "demo" {!cir.int<s, 32>}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testcases should use regex for FileCheck, see example here: clang/test/CIR/CodeGen/abstract-cond.c

// CHECK: !ty_22demo2E122 = !cir.struct<struct "demo.1" {!cir.int<s, 32>, !cir.int<s, 32>}>
// CHECK: cir.func no_proto @testOne() extra(#fn_attr) {
// CHECK: %0 = cir.alloca !ty_22demo22, !cir.ptr<!ty_22demo22>, ["one"] {alignment = 4 : i64}
// CHECK: %1 = cir.const #cir.const_struct<{#cir.int<1> : !s32i}> : !ty_22demo22
// CHECK: cir.store %1, %0 : !ty_22demo22, !cir.ptr<!ty_22demo22>
// CHECK: cir.return
// CHECK: }
// CHECK: cir.func no_proto @testTwo() extra(#fn_attr) {
// CHECK: %0 = cir.alloca !ty_22demo2E122, !cir.ptr<!ty_22demo2E122>, ["two"] {alignment = 4 : i64}
// CHECK: %1 = cir.const #cir.const_struct<{#cir.int<1> : !s32i, #cir.int<2> : !s32i}> : !ty_22demo2E122
// CHECK: cir.store %1, %0 : !ty_22demo2E122, !cir.ptr<!ty_22demo2E122>
// CHECK: cir.return
// CHECK: }