Skip to content

Commit 0f17161

Browse files
authored
Create clang::MangleContext once per file instead of once per C++ thunk (#5924)
Part of #5514.
1 parent 55085de commit 0f17161

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

toolchain/check/cpp_thunk.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,11 @@ namespace Carbon::Check {
2020

2121
// Returns the C++ thunk mangled name given the callee function.
2222
static auto GenerateThunkMangledName(
23-
clang::ASTContext& ast_context,
23+
clang::MangleContext& mangle_context,
2424
const clang::FunctionDecl& callee_function_decl) -> std::string {
2525
RawStringOstream mangled_name_stream;
26-
{
27-
// TODO: Create `MangleContext` once.
28-
std::unique_ptr<clang::MangleContext> mangle_context(
29-
ast_context.createMangleContext());
30-
mangle_context->mangleName(clang::GlobalDecl(&callee_function_decl),
31-
mangled_name_stream);
32-
}
33-
26+
mangle_context.mangleName(clang::GlobalDecl(&callee_function_decl),
27+
mangled_name_stream);
3428
mangled_name_stream << ".carbon_thunk";
3529

3630
return mangled_name_stream.TakeStr();
@@ -185,9 +179,9 @@ static auto BuildThunkParameters(
185179
// Returns the thunk function declaration given the callee function and the
186180
// thunk parameter types.
187181
static auto CreateThunkFunctionDecl(
188-
clang::ASTContext& ast_context,
189-
const clang::FunctionDecl& callee_function_decl,
182+
Context& context, const clang::FunctionDecl& callee_function_decl,
190183
llvm::ArrayRef<clang::QualType> thunk_param_types) -> clang::FunctionDecl* {
184+
clang::ASTContext& ast_context = context.ast_context();
191185
clang::SourceLocation clang_loc = callee_function_decl.getLocation();
192186

193187
clang::IdentifierInfo& identifier_info = ast_context.Idents.get(
@@ -216,7 +210,9 @@ static auto CreateThunkFunctionDecl(
216210

217211
// Set asm("<callee function mangled name>.carbon_thunk").
218212
thunk_function_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
219-
ast_context, GenerateThunkMangledName(ast_context, callee_function_decl),
213+
ast_context,
214+
GenerateThunkMangledName(*context.sem_ir().clang_mangle_context(),
215+
callee_function_decl),
220216
clang_loc));
221217

222218
return thunk_function_decl;
@@ -278,8 +274,6 @@ static auto BuildThunkBody(clang::Sema& sema,
278274

279275
auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
280276
-> clang::FunctionDecl* {
281-
clang::ASTContext& ast_context = context.ast_context();
282-
283277
clang::FunctionDecl* callee_function_decl =
284278
context.sem_ir()
285279
.clang_decls()
@@ -289,9 +283,9 @@ auto BuildCppThunk(Context& context, const SemIR::Function& callee_function)
289283

290284
// Build the thunk function declaration.
291285
auto [thunk_param_types, param_type_changed] =
292-
BuildThunkParameterTypes(ast_context, *callee_function_decl);
286+
BuildThunkParameterTypes(context.ast_context(), *callee_function_decl);
293287
clang::FunctionDecl* thunk_function_decl = CreateThunkFunctionDecl(
294-
ast_context, *callee_function_decl, thunk_param_types);
288+
context, *callee_function_decl, thunk_param_types);
295289

296290
// Build the thunk function body.
297291
clang::Sema& sema = context.sem_ir().cpp_ast()->getSema();

toolchain/sem_ir/file.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99
#include <utility>
1010

11+
#include "clang/AST/Mangle.h"
1112
#include "common/check.h"
1213
#include "llvm/ADT/STLExtras.h"
1314
#include "llvm/ADT/SmallVector.h"
@@ -54,6 +55,8 @@ File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
5455
}
5556
}
5657

58+
File::~File() = default;
59+
5760
auto File::Verify() const -> ErrorOr<Success> {
5861
// Invariants don't necessarily hold for invalid IR.
5962
if (has_errors_) {
@@ -147,4 +150,9 @@ auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
147150
mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
148151
}
149152

153+
auto File::set_cpp_ast(clang::ASTUnit* cpp_ast) -> void {
154+
cpp_ast_ = cpp_ast;
155+
clang_mangle_context_.reset(cpp_ast->getASTContext().createMangleContext());
156+
}
157+
150158
} // namespace Carbon::SemIR

toolchain/sem_ir/file.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class File : public Printable<File> {
7676
SharedValueStores& value_stores, std::string filename);
7777

7878
File(const File&) = delete;
79+
~File();
7980
auto operator=(const File&) -> File& = delete;
8081

8182
// Verifies that invariants of the semantics IR hold.
@@ -198,7 +199,10 @@ class File : public Printable<File> {
198199
// TODO: When the AST can be created before creating `File`, initialize the
199200
// pointer in the constructor and remove this function. This is part of
200201
// https://github.com/carbon-language/carbon-lang/issues/4666
201-
auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void { cpp_ast_ = cpp_ast; }
202+
auto set_cpp_ast(clang::ASTUnit* cpp_ast) -> void;
203+
auto clang_mangle_context() -> clang::MangleContext* {
204+
return clang_mangle_context_.get();
205+
}
202206
auto clang_decls() -> ClangDeclStore& { return clang_decls_; }
203207
auto clang_decls() const -> const ClangDeclStore& { return clang_decls_; }
204208
auto names() const -> NameStoreWrapper {
@@ -333,6 +337,10 @@ class File : public Printable<File> {
333337
// `Cpp` imports.
334338
clang::ASTUnit* cpp_ast_ = nullptr;
335339

340+
// The Clang mangle context for the target in the ASTContext. Initialized
341+
// together with `cpp_ast_`.
342+
std::unique_ptr<clang::MangleContext> clang_mangle_context_;
343+
336344
// Clang AST declarations pointing to the AST and their mapped Carbon
337345
// instructions. When calling `Lookup()`, `inst_id` is ignored. `Add()` will
338346
// not add multiple entries with the same `decl` and different `inst_id`.

0 commit comments

Comments
 (0)