Reuse LLVM global constants. (#5273)

Reuse LLVM global constants: do not generate a new llvm::Global constant
for a pointer to a global constant for the same Carbon const_inst_id,
reuse already generated one.
This commit is contained in:
Alina Sbirlea
2025-04-09 16:36:42 +00:00
committed by GitHub
parent 20444c0103
commit 694a329bdd
3 changed files with 10 additions and 10 deletions
+7 -4
View File
@@ -154,9 +154,9 @@ auto FileContext::GetGlobal(SemIR::InstId inst_id,
return const_value;
}
// If we want a pointer to the constant, materialize a global to hold it.
// TODO: We could reuse the same global if the constant is used more than
// once.
if (auto result = global_variables().Lookup(const_inst_id)) {
return result.value();
}
// Include both the name of the constant, if any, and the point of use in
// the name of the variable.
@@ -177,10 +177,13 @@ auto FileContext::GetGlobal(SemIR::InstId inst_id,
}
llvm::StringRef sep = (use_name[0] == '.') ? "" : ".";
return new llvm::GlobalVariable(
auto* global_variable = new llvm::GlobalVariable(
llvm_module(), GetType(sem_ir().GetPointeeType(value_rep.type_id)),
/*isConstant=*/true, llvm::GlobalVariable::InternalLinkage, const_value,
const_name + sep + use_name);
global_variables_.Insert(const_inst_id, global_variable);
return global_variable;
}
auto FileContext::GetOrCreateFunction(SemIR::FunctionId function_id,