Skip to content

Commit 782ab83

Browse files
authored
[CIR] Set the module name to the input filename (#160934)
This sets the MLIR module name to the main filename (according to the SourceManager), if one is available. The module name gets used when creating global init functions, so we will need it to be set.
1 parent 0251fd9 commit 782ab83

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
119119
cir::OptInfoAttr::get(&mlirContext,
120120
cgo.OptimizationLevel,
121121
cgo.OptimizeSize));
122+
// Set the module name to be the name of the main file. TranslationUnitDecl
123+
// often contains invalid source locations and isn't a reliable source for the
124+
// module location.
125+
FileID mainFileId = astContext.getSourceManager().getMainFileID();
126+
const FileEntry &mainFile =
127+
*astContext.getSourceManager().getFileEntryForID(mainFileId);
128+
StringRef path = mainFile.tryGetRealPathName();
129+
if (!path.empty()) {
130+
theModule.setSymName(path);
131+
theModule->setLoc(mlir::FileLineColLoc::get(&mlirContext, path,
132+
/*line=*/0,
133+
/*column=*/0));
134+
}
122135
}
123136

124137
CIRGenModule::~CIRGenModule() = default;

clang/test/CIR/CodeGen/lang-c-cpp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// RUN: %clang_cc1 -x c -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.c.cir
44
// RUN: FileCheck --check-prefix=CIR-C --input-file=%t.c.cir %s
55

6-
// CIR-CPP: module attributes {{{.*}}cir.lang = #cir.lang<cxx>{{.*}}}
7-
// CIR-C: module attributes {{{.*}}cir.lang = #cir.lang<c>{{.*}}}
6+
// CIR-CPP: module{{.*}} attributes {{{.*}}cir.lang = #cir.lang<cxx>{{.*}}}
7+
// CIR-C: module{{.*}} attributes {{{.*}}cir.lang = #cir.lang<c>{{.*}}}
88

99
int main() {
1010
return 0;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
4+
// Normally, we try to avoid checking the filename of a test, but that's the
5+
// entire point of this test, so we use a wildcard for the path but check the
6+
// filename.
7+
// CIR: module @"{{.*}}module-filename.cpp"
8+
9+
int main() {
10+
return 0;
11+
}

clang/test/CIR/CodeGen/opt-info-attr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
void f() {}
1515

16-
// CHECK-O0: module attributes
16+
// CHECK-O0: module{{.*}} attributes
1717
// CHECK-O0-NOT: cir.opt_info
18-
// CHECK-O1: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 1, size = 0>{{.+}}
19-
// CHECK-O2: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 0>{{.+}}
20-
// CHECK-O3: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 3, size = 0>{{.+}}
21-
// CHECK-Os: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 1>{{.+}}
22-
// CHECK-Oz: module attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 2>{{.+}}
18+
// CHECK-O1: module{{.*}} attributes {{.+}}cir.opt_info = #cir.opt_info<level = 1, size = 0>{{.+}}
19+
// CHECK-O2: module{{.*}} attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 0>{{.+}}
20+
// CHECK-O3: module{{.*}} attributes {{.+}}cir.opt_info = #cir.opt_info<level = 3, size = 0>{{.+}}
21+
// CHECK-Os: module{{.*}} attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 1>{{.+}}
22+
// CHECK-Oz: module{{.*}} attributes {{.+}}cir.opt_info = #cir.opt_info<level = 2, size = 2>{{.+}}

0 commit comments

Comments
 (0)