Skip to content

Commit

Permalink
make many global variables constant (#1167)
Browse files Browse the repository at this point in the history
This PR is a preparation to improve the thread safety of the llvm
backend which attempts to reduce the number of global variables that
need to be considered by marking as many of them as read only as
possible.
  • Loading branch information
dwightguth authored Nov 26, 2024
1 parent 5e07327 commit f18f71c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions config/llvm_header.inc
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ declare void @print_configuration(ptr, ptr)
declare i64 @__gmpz_get_ui(ptr)
@exit_int_0 = global %mpz { i32 0, i32 0, ptr getelementptr inbounds ([0 x i64], ptr @exit_int_0_limbs, i32 0, i32 0) }
@exit_int_0_limbs = global [0 x i64] zeroinitializer
@exit_int_0 = constant %mpz { i32 0, i32 0, ptr getelementptr inbounds ([0 x i64], ptr @exit_int_0_limbs, i32 0, i32 0) }
@exit_int_0_limbs = constant [0 x i64] zeroinitializer
define tailcc ptr @"eval_LblgetExitCode{SortGeneratedTopCell{}}"(ptr) {
ret ptr @exit_int_0
Expand All @@ -110,8 +110,8 @@ declare ptr @hook_INT_add(ptr, ptr)
declare ptr @evaluate_function_symbol(i32, ptr)
declare ptr @get_terminated_string(ptr)
@fresh_int_1 = global %mpz { i32 1, i32 1, ptr getelementptr inbounds ([1 x i64], ptr @fresh_int_1_limbs, i32 0, i32 0) }
@fresh_int_1_limbs = global [1 x i64] [i64 1]
@fresh_int_1 = constant %mpz { i32 1, i32 1, ptr getelementptr inbounds ([1 x i64], ptr @fresh_int_1_limbs, i32 0, i32 0) }
@fresh_int_1_limbs = constant [1 x i64] [i64 1]
define ptr @get_fresh_constant(ptr %sort, ptr %top) {
entry:
Expand Down
1 change: 1 addition & 0 deletions include/runtime/types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RUNTIME_TYPES_H
#define RUNTIME_TYPES_H

#include <cstdint>
#include <gmp.h>
#include <mpfr.h>

Expand Down
8 changes: 8 additions & 0 deletions lib/codegen/CreateStaticTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ llvm::Constant *create_static_term::not_injection_case(
llvm::Constant *block
= module_->getOrInsertGlobal(kore_string.str(), block_type);
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(block);
// this is technically not a constant because functions which return fresh constants
// will mutate a block in this circumstance. Probably best not to rely on this actually
// being mutable any other way.

if (!global_var->hasInitializer()) {
std::vector<llvm::Constant *> block_vals;
Expand Down Expand Up @@ -151,6 +154,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
"int_" + contents, llvm::StructType::getTypeByName(
module_->getContext(), int_wrapper_struct));
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
mpz_t value;
char const *data_start
Expand All @@ -163,6 +167,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
llvm::Constant *limbs = module_->getOrInsertGlobal(
"int_" + contents + "_limbs", limbs_type);
auto *limbs_var = llvm::dyn_cast<llvm::GlobalVariable>(limbs);
limbs_var->setConstant(true);
std::vector<llvm::Constant *> allocd_limbs;
for (size_t i = 0; i < size; i++) {
allocd_limbs.push_back(llvm::ConstantInt::get(
Expand Down Expand Up @@ -205,6 +210,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
"float_" + contents, llvm::StructType::getTypeByName(
module_->getContext(), float_wrapper_struct));
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
size_t prec = 0;
size_t exp = 0;
Expand Down Expand Up @@ -246,6 +252,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
llvm::Constant *limbs = module_->getOrInsertGlobal(
"float_" + contents + "_limbs", limbs_type);
auto *limbs_var = llvm::dyn_cast<llvm::GlobalVariable>(limbs);
limbs_var->setConstant(true);
std::vector<llvm::Constant *> allocd_limbs;
for (size_t i = 0; i < size; i++) {
allocd_limbs.push_back(llvm::ConstantInt::get(
Expand Down Expand Up @@ -317,6 +324,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
llvm::Constant *global
= module_->getOrInsertGlobal("token_" + escape(contents), string_type);
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
llvm::StructType *block_header_type = llvm::StructType::getTypeByName(
module_->getContext(), blockheader_struct);
Expand Down
2 changes: 2 additions & 0 deletions lib/codegen/Decision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ llvm::Constant *decision::string_literal(std::string const &str) {
auto *global
= module_->getOrInsertGlobal("str_lit_" + str, str_cst->getType());
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str_cst);
}
Expand Down Expand Up @@ -1091,6 +1092,7 @@ std::pair<std::vector<llvm::Value *>, llvm::BasicBlock *> step_function_header(
auto *layout = module->getOrInsertGlobal(
"layout_item_rule_" + std::to_string(ordinal), layout_arr->getType());
auto *global_var = llvm::cast<llvm::GlobalVariable>(layout);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(layout_arr);
}
Expand Down
13 changes: 13 additions & 0 deletions lib/codegen/EmitConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static llvm::Constant *get_symbol_name_ptr(
auto *global = module->getOrInsertGlobal(
fmt::format("sym_name_{}", name), str->getType());
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down Expand Up @@ -144,6 +145,7 @@ static void emit_data_table_for_symbol(
auto *table_type = llvm::ArrayType::get(ty, syms.size());
auto *table = module->getOrInsertGlobal("table_" + name, table_type);
auto *global_var = llvm::cast<llvm::GlobalVariable>(table);
global_var->setConstant(true);
init_debug_global(
"table_" + name,
get_array_debug_type(
Expand Down Expand Up @@ -437,6 +439,7 @@ emit_get_tag_for_fresh_sort(kore_definition *definition, llvm::Module *module) {
auto *global
= module->getOrInsertGlobal("sort_name_" + name, str->getType());
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down Expand Up @@ -504,6 +507,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
auto *global
= module->getOrInsertGlobal("sort_name_" + name, str->getType());
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down Expand Up @@ -531,6 +535,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
auto *str = llvm::ConstantDataArray::getString(ctx, "true", false);
auto *global = module->getOrInsertGlobal("bool_true", str->getType());
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down Expand Up @@ -625,6 +630,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
string_type, block, {zero, zero32, zero32}, "", current_block);
auto *block_size = module->getOrInsertGlobal(
"VAR_BLOCK_SIZE", llvm::Type::getInt64Ty(ctx));
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(block_size);
global_var->setConstant(true);
auto *block_size_val = new llvm::LoadInst(
llvm::Type::getInt64Ty(ctx), block_size, "", current_block);
auto *block_alloc_size = llvm::BinaryOperator::Create(
Expand Down Expand Up @@ -904,6 +911,7 @@ static void get_visitor(
auto *global = module->getOrInsertGlobal(
fmt::format("sort_name_{}", sort_name), str->getType());
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down Expand Up @@ -1097,6 +1105,7 @@ static llvm::Constant *get_layout_data(
auto *global = module->getOrInsertGlobal(
"layout_item_" + std::to_string(layout), arr->getType());
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(arr);
}
Expand All @@ -1109,6 +1118,7 @@ static llvm::Constant *get_layout_data(
name,
llvm::StructType::getTypeByName(module->getContext(), layout_struct));
auto *global_var2 = llvm::cast<llvm::GlobalVariable>(global2);
global_var2->setConstant(true);
init_debug_global(name, get_forward_decl(layout_struct), global_var2);
if (!global_var2->hasInitializer()) {
global_var2->setInitializer(llvm::ConstantStruct::get(
Expand Down Expand Up @@ -1201,6 +1211,7 @@ static void emit_sort_table_for_proof_trace_serialization(
auto *subtable = module->getOrInsertGlobal(
fmt::format("sort_tags_{}", ast_to_string(*symbol)), subtable_type);
auto *subtable_var = llvm::dyn_cast<llvm::GlobalVariable>(subtable);
subtable_var->setConstant(true);
init_debug_global(
"sort_tags_" + symbol->get_name(),
get_array_debug_type(
Expand Down Expand Up @@ -1246,6 +1257,7 @@ static void emit_sort_table(kore_definition *def, llvm::Module *mod) {
auto *subtable = module->getOrInsertGlobal(
fmt::format("sorts_{}", ast_to_string(*symbol)), subtable_type);
auto *subtable_var = llvm::dyn_cast<llvm::GlobalVariable>(subtable);
subtable_var->setConstant(true);
init_debug_global(
"sorts_" + symbol->get_name(),
get_array_debug_type(
Expand Down Expand Up @@ -1304,6 +1316,7 @@ static void emit_return_sort_table(kore_definition *def, llvm::Module *mod) {
auto *sort_name
= module->getOrInsertGlobal("sort_name_" + sort_str, str_type);
auto *global_var = llvm::cast<llvm::GlobalVariable>(sort_name);
global_var->setConstant(true);
if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/codegen/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void add_boolean_flag(

auto *global = mod.getOrInsertGlobal(name, i1_ty);
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);

if (!global_var->hasInitializer()) {
global_var->setInitializer(enabled_cst);
Expand All @@ -44,6 +45,7 @@ void add_kompiled_dir_symbol(

auto *global = mod.getOrInsertGlobal(kompiled_dir, str->getType());
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
global_var->setConstant(true);

if (!global_var->hasInitializer()) {
global_var->setInitializer(str);
Expand Down

0 comments on commit f18f71c

Please sign in to comment.