Skip to content

Commit f18f71c

Browse files
authored
make many global variables constant (#1167)
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.
1 parent 5e07327 commit f18f71c

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

config/llvm_header.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ declare void @print_configuration(ptr, ptr)
8282
8383
declare i64 @__gmpz_get_ui(ptr)
8484
85-
@exit_int_0 = global %mpz { i32 0, i32 0, ptr getelementptr inbounds ([0 x i64], ptr @exit_int_0_limbs, i32 0, i32 0) }
86-
@exit_int_0_limbs = global [0 x i64] zeroinitializer
85+
@exit_int_0 = constant %mpz { i32 0, i32 0, ptr getelementptr inbounds ([0 x i64], ptr @exit_int_0_limbs, i32 0, i32 0) }
86+
@exit_int_0_limbs = constant [0 x i64] zeroinitializer
8787
8888
define tailcc ptr @"eval_LblgetExitCode{SortGeneratedTopCell{}}"(ptr) {
8989
ret ptr @exit_int_0
@@ -110,8 +110,8 @@ declare ptr @hook_INT_add(ptr, ptr)
110110
declare ptr @evaluate_function_symbol(i32, ptr)
111111
declare ptr @get_terminated_string(ptr)
112112
113-
@fresh_int_1 = global %mpz { i32 1, i32 1, ptr getelementptr inbounds ([1 x i64], ptr @fresh_int_1_limbs, i32 0, i32 0) }
114-
@fresh_int_1_limbs = global [1 x i64] [i64 1]
113+
@fresh_int_1 = constant %mpz { i32 1, i32 1, ptr getelementptr inbounds ([1 x i64], ptr @fresh_int_1_limbs, i32 0, i32 0) }
114+
@fresh_int_1_limbs = constant [1 x i64] [i64 1]
115115
116116
define ptr @get_fresh_constant(ptr %sort, ptr %top) {
117117
entry:

include/runtime/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RUNTIME_TYPES_H
22
#define RUNTIME_TYPES_H
33

4+
#include <cstdint>
45
#include <gmp.h>
56
#include <mpfr.h>
67

lib/codegen/CreateStaticTerm.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ llvm::Constant *create_static_term::not_injection_case(
3939
llvm::Constant *block
4040
= module_->getOrInsertGlobal(kore_string.str(), block_type);
4141
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(block);
42+
// this is technically not a constant because functions which return fresh constants
43+
// will mutate a block in this circumstance. Probably best not to rely on this actually
44+
// being mutable any other way.
4245

4346
if (!global_var->hasInitializer()) {
4447
std::vector<llvm::Constant *> block_vals;
@@ -151,6 +154,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
151154
"int_" + contents, llvm::StructType::getTypeByName(
152155
module_->getContext(), int_wrapper_struct));
153156
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
157+
global_var->setConstant(true);
154158
if (!global_var->hasInitializer()) {
155159
mpz_t value;
156160
char const *data_start
@@ -163,6 +167,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
163167
llvm::Constant *limbs = module_->getOrInsertGlobal(
164168
"int_" + contents + "_limbs", limbs_type);
165169
auto *limbs_var = llvm::dyn_cast<llvm::GlobalVariable>(limbs);
170+
limbs_var->setConstant(true);
166171
std::vector<llvm::Constant *> allocd_limbs;
167172
for (size_t i = 0; i < size; i++) {
168173
allocd_limbs.push_back(llvm::ConstantInt::get(
@@ -205,6 +210,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
205210
"float_" + contents, llvm::StructType::getTypeByName(
206211
module_->getContext(), float_wrapper_struct));
207212
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
213+
global_var->setConstant(true);
208214
if (!global_var->hasInitializer()) {
209215
size_t prec = 0;
210216
size_t exp = 0;
@@ -246,6 +252,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
246252
llvm::Constant *limbs = module_->getOrInsertGlobal(
247253
"float_" + contents + "_limbs", limbs_type);
248254
auto *limbs_var = llvm::dyn_cast<llvm::GlobalVariable>(limbs);
255+
limbs_var->setConstant(true);
249256
std::vector<llvm::Constant *> allocd_limbs;
250257
for (size_t i = 0; i < size; i++) {
251258
allocd_limbs.push_back(llvm::ConstantInt::get(
@@ -317,6 +324,7 @@ create_static_term::create_token(value_type sort, std::string contents) {
317324
llvm::Constant *global
318325
= module_->getOrInsertGlobal("token_" + escape(contents), string_type);
319326
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
327+
global_var->setConstant(true);
320328
if (!global_var->hasInitializer()) {
321329
llvm::StructType *block_header_type = llvm::StructType::getTypeByName(
322330
module_->getContext(), blockheader_struct);

lib/codegen/Decision.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ llvm::Constant *decision::string_literal(std::string const &str) {
694694
auto *global
695695
= module_->getOrInsertGlobal("str_lit_" + str, str_cst->getType());
696696
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
697+
global_var->setConstant(true);
697698
if (!global_var->hasInitializer()) {
698699
global_var->setInitializer(str_cst);
699700
}
@@ -1091,6 +1092,7 @@ std::pair<std::vector<llvm::Value *>, llvm::BasicBlock *> step_function_header(
10911092
auto *layout = module->getOrInsertGlobal(
10921093
"layout_item_rule_" + std::to_string(ordinal), layout_arr->getType());
10931094
auto *global_var = llvm::cast<llvm::GlobalVariable>(layout);
1095+
global_var->setConstant(true);
10941096
if (!global_var->hasInitializer()) {
10951097
global_var->setInitializer(layout_arr);
10961098
}

lib/codegen/EmitConfigParser.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static llvm::Constant *get_symbol_name_ptr(
5555
auto *global = module->getOrInsertGlobal(
5656
fmt::format("sym_name_{}", name), str->getType());
5757
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
58+
global_var->setConstant(true);
5859
if (!global_var->hasInitializer()) {
5960
global_var->setInitializer(str);
6061
}
@@ -144,6 +145,7 @@ static void emit_data_table_for_symbol(
144145
auto *table_type = llvm::ArrayType::get(ty, syms.size());
145146
auto *table = module->getOrInsertGlobal("table_" + name, table_type);
146147
auto *global_var = llvm::cast<llvm::GlobalVariable>(table);
148+
global_var->setConstant(true);
147149
init_debug_global(
148150
"table_" + name,
149151
get_array_debug_type(
@@ -437,6 +439,7 @@ emit_get_tag_for_fresh_sort(kore_definition *definition, llvm::Module *module) {
437439
auto *global
438440
= module->getOrInsertGlobal("sort_name_" + name, str->getType());
439441
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
442+
global_var->setConstant(true);
440443
if (!global_var->hasInitializer()) {
441444
global_var->setInitializer(str);
442445
}
@@ -504,6 +507,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
504507
auto *global
505508
= module->getOrInsertGlobal("sort_name_" + name, str->getType());
506509
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
510+
global_var->setConstant(true);
507511
if (!global_var->hasInitializer()) {
508512
global_var->setInitializer(str);
509513
}
@@ -531,6 +535,7 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
531535
auto *str = llvm::ConstantDataArray::getString(ctx, "true", false);
532536
auto *global = module->getOrInsertGlobal("bool_true", str->getType());
533537
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
538+
global_var->setConstant(true);
534539
if (!global_var->hasInitializer()) {
535540
global_var->setInitializer(str);
536541
}
@@ -625,6 +630,8 @@ static void emit_get_token(kore_definition *definition, llvm::Module *module) {
625630
string_type, block, {zero, zero32, zero32}, "", current_block);
626631
auto *block_size = module->getOrInsertGlobal(
627632
"VAR_BLOCK_SIZE", llvm::Type::getInt64Ty(ctx));
633+
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(block_size);
634+
global_var->setConstant(true);
628635
auto *block_size_val = new llvm::LoadInst(
629636
llvm::Type::getInt64Ty(ctx), block_size, "", current_block);
630637
auto *block_alloc_size = llvm::BinaryOperator::Create(
@@ -904,6 +911,7 @@ static void get_visitor(
904911
auto *global = module->getOrInsertGlobal(
905912
fmt::format("sort_name_{}", sort_name), str->getType());
906913
auto *global_var = llvm::dyn_cast<llvm::GlobalVariable>(global);
914+
global_var->setConstant(true);
907915
if (!global_var->hasInitializer()) {
908916
global_var->setInitializer(str);
909917
}
@@ -1097,6 +1105,7 @@ static llvm::Constant *get_layout_data(
10971105
auto *global = module->getOrInsertGlobal(
10981106
"layout_item_" + std::to_string(layout), arr->getType());
10991107
auto *global_var = llvm::cast<llvm::GlobalVariable>(global);
1108+
global_var->setConstant(true);
11001109
if (!global_var->hasInitializer()) {
11011110
global_var->setInitializer(arr);
11021111
}
@@ -1109,6 +1118,7 @@ static llvm::Constant *get_layout_data(
11091118
name,
11101119
llvm::StructType::getTypeByName(module->getContext(), layout_struct));
11111120
auto *global_var2 = llvm::cast<llvm::GlobalVariable>(global2);
1121+
global_var2->setConstant(true);
11121122
init_debug_global(name, get_forward_decl(layout_struct), global_var2);
11131123
if (!global_var2->hasInitializer()) {
11141124
global_var2->setInitializer(llvm::ConstantStruct::get(
@@ -1201,6 +1211,7 @@ static void emit_sort_table_for_proof_trace_serialization(
12011211
auto *subtable = module->getOrInsertGlobal(
12021212
fmt::format("sort_tags_{}", ast_to_string(*symbol)), subtable_type);
12031213
auto *subtable_var = llvm::dyn_cast<llvm::GlobalVariable>(subtable);
1214+
subtable_var->setConstant(true);
12041215
init_debug_global(
12051216
"sort_tags_" + symbol->get_name(),
12061217
get_array_debug_type(
@@ -1246,6 +1257,7 @@ static void emit_sort_table(kore_definition *def, llvm::Module *mod) {
12461257
auto *subtable = module->getOrInsertGlobal(
12471258
fmt::format("sorts_{}", ast_to_string(*symbol)), subtable_type);
12481259
auto *subtable_var = llvm::dyn_cast<llvm::GlobalVariable>(subtable);
1260+
subtable_var->setConstant(true);
12491261
init_debug_global(
12501262
"sorts_" + symbol->get_name(),
12511263
get_array_debug_type(
@@ -1304,6 +1316,7 @@ static void emit_return_sort_table(kore_definition *def, llvm::Module *mod) {
13041316
auto *sort_name
13051317
= module->getOrInsertGlobal("sort_name_" + sort_str, str_type);
13061318
auto *global_var = llvm::cast<llvm::GlobalVariable>(sort_name);
1319+
global_var->setConstant(true);
13071320
if (!global_var->hasInitializer()) {
13081321
global_var->setInitializer(str);
13091322
}

lib/codegen/Metadata.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ void add_boolean_flag(
2424

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

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

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

4850
if (!global_var->hasInitializer()) {
4951
global_var->setInitializer(str);

0 commit comments

Comments
 (0)