Skip to content

Commit 252e074

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents d910a26 + c2e8094 commit 252e074

File tree

11 files changed

+141
-24
lines changed

11 files changed

+141
-24
lines changed

include/kllvm/codegen/ApplyPasses.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace kllvm {
88

9-
void do_bitcode_linking(llvm::Module &);
9+
void do_bitcode_linking(llvm::Module &, char *, unsigned);
1010

1111
void apply_kllvm_opt_passes(llvm::Module &, bool hidden_visibility);
1212

13+
void apply_inline_pass(llvm::Module &);
14+
1315
void generate_object_file(llvm::Module &, llvm::raw_ostream &);
1416

1517
} // namespace kllvm

include/kllvm/codegen/CreateTerm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ llvm::Value *allocate_term(
164164
llvm::Value *allocate_term(
165165
llvm::Type *alloc_type, llvm::BasicBlock *block,
166166
char const *alloc_fn = "kore_alloc", bool mergeable = false);
167+
llvm::Value *addrspace_cast0_to0(
168+
llvm::Module *module, llvm::Value *val, llvm::BasicBlock *block);
167169
} // namespace kllvm
168170

169171
#endif // CREATE_TERM_H

include/runtime/opaque_cpp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef OPAQUE_CPP_H
2+
#define OPAQUE_CPP_H
3+
4+
/* This header file declares a string containing the textual bitcode of
5+
* runtime/opaque/opaque.ll. The reason we use a separate IR file for the
6+
* functions in opaque.ll is that the instructions they include confuse the
7+
* RewriteStatepointsForGC pass. Therefore, we first perform all the regular
8+
* optimization passes, then link, and then run the inline pass again.
9+
*/
10+
extern unsigned char opaque_ll[];
11+
extern unsigned int opaque_ll_len;
12+
13+
#endif // define OPAQUE_CPP_H

lib/codegen/ApplyPasses.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <kllvm/codegen/RemoveDeadKFunctions.h>
55
#include <kllvm/codegen/SetVisibilityHidden.h>
66

7-
#include "runtime/alloc_cpp.h"
87
#include "runtime/header.h"
98

109
#if LLVM_VERSION_MAJOR >= 17
@@ -16,6 +15,7 @@
1615
#endif
1716

1817
#include "llvm/IRReader/IRReader.h"
18+
#include "llvm/Transforms/IPO/AlwaysInliner.h"
1919
#include <llvm/Bitcode/BitcodeReader.h>
2020
#include <llvm/IR/LegacyPassManager.h>
2121
#include <llvm/Linker/Linker.h>
@@ -114,6 +114,38 @@ void apply_kllvm_opt_passes(llvm::Module &mod, bool hidden_visibility) {
114114
mpm.run(mod, mam);
115115
}
116116

117+
void apply_inline_pass(llvm::Module &mod) {
118+
// Create the analysis managers.
119+
// These must be declared in this order so that they are destroyed in the
120+
// correct order due to inter-analysis-manager references.
121+
LoopAnalysisManager lam;
122+
FunctionAnalysisManager fam;
123+
CGSCCAnalysisManager cgam;
124+
ModuleAnalysisManager mam;
125+
126+
// Create the new pass manager builder.
127+
// Take a look at the PassBuilder constructor parameters for more
128+
// customization, e.g. specifying a TargetMachine or various debugging
129+
// options.
130+
PassBuilder pb;
131+
132+
// Register all the basic analyses with the managers.
133+
pb.registerModuleAnalyses(mam);
134+
pb.registerCGSCCAnalyses(cgam);
135+
pb.registerFunctionAnalyses(fam);
136+
pb.registerLoopAnalyses(lam);
137+
pb.crossRegisterProxies(lam, fam, cgam, mam);
138+
139+
// Create the pass manager.
140+
ModulePassManager mpm;
141+
142+
// Add always inline pass
143+
mpm.addPass(AlwaysInlinerPass());
144+
145+
// Run always inline pass
146+
mpm.run(mod, mam);
147+
}
148+
117149
void generate_object_file(llvm::Module &mod, llvm::raw_ostream &os) {
118150
if (keep_frame_pointer) {
119151
mod.setFramePointer(FramePointerKind::All);
@@ -167,14 +199,13 @@ void generate_object_file(llvm::Module &mod, llvm::raw_ostream &os) {
167199
* is done currently with only a single file: runtime/lto/alloc.cpp. We do this
168200
* so that inlining can occur across the functions in each file.
169201
*/
170-
void do_bitcode_linking(llvm::Module &mod) {
202+
void do_bitcode_linking(llvm::Module &mod, char *bc, unsigned bc_len) {
171203
Linker linker(mod);
172204
llvm::SMDiagnostic err;
173-
auto alloc_cpp_mod = llvm::parseIR(
174-
*llvm::MemoryBuffer::getMemBuffer(
175-
std::string((char *)alloc_cpp_o_ll, alloc_cpp_o_ll_len)),
176-
err, mod.getContext());
177-
bool error = linker.linkInModule(std::move(alloc_cpp_mod));
205+
auto cpp_mod = llvm::parseIR(
206+
*llvm::MemoryBuffer::getMemBuffer(std::string(bc, bc_len)), err,
207+
mod.getContext());
208+
bool error = linker.linkInModule(std::move(cpp_mod));
178209
if (error) {
179210
throw std::runtime_error(
180211
"Bitcode linking failed. Please report this as a bug.");

lib/codegen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ add_library(Codegen
1313
)
1414

1515
target_link_libraries(Codegen
16-
PUBLIC AST fmt::fmt-header-only alloc-cpp
16+
PUBLIC AST fmt::fmt-header-only alloc-cpp opaque-cpp
1717
PRIVATE base64)

lib/codegen/CreateTerm.cpp

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ llvm::Type *get_param_type(value_type sort, llvm::Module *module) {
6464
case sort_category::RangeMap:
6565
case sort_category::List:
6666
case sort_category::Set:
67-
type = llvm::PointerType::getUnqual(module->getContext());
67+
type = use_gcstrategy ? llvm::PointerType::get(module->getContext(), 0)
68+
: llvm::PointerType::getUnqual(module->getContext());
6869
break;
6970
default: break;
7071
}
@@ -94,6 +95,9 @@ llvm::Type *getvalue_type(value_type sort, llvm::Module *module) {
9495
case sort_category::StringBuffer:
9596
case sort_category::Symbol:
9697
case sort_category::Variable:
98+
if (use_gcstrategy) {
99+
return llvm::PointerType::get(module->getContext(), 0);
100+
}
97101
return llvm::PointerType::getUnqual(module->getContext());
98102
case sort_category::MapIter:
99103
case sort_category::SetIter:
@@ -169,6 +173,24 @@ llvm::Value *get_block_header(
169173
llvm::Type::getInt64Ty(module->getContext()), header_val));
170174
}
171175

176+
static llvm::Value *addrspace_cast(
177+
llvm::Module *module, llvm::Value *val, llvm::BasicBlock *block, int from,
178+
int to) {
179+
std::string name
180+
= "addrspace_" + std::to_string(from) + "_to_" + std::to_string(to);
181+
auto *addrspace = llvm::CallInst::Create(
182+
get_or_insert_function(
183+
module, name, llvm::PointerType::get(module->getContext(), to),
184+
llvm::PointerType::get(module->getContext(), from)),
185+
{val}, "", block);
186+
return addrspace;
187+
}
188+
189+
llvm::Value *addrspace_cast0_to0(
190+
llvm::Module *module, llvm::Value *val, llvm::BasicBlock *block) {
191+
return addrspace_cast(module, val, block, 0, 0);
192+
}
193+
172194
template <typename T>
173195
requires std::same_as<T, llvm::BasicBlock>
174196
|| std::same_as<T, llvm::Instruction>
@@ -910,12 +932,17 @@ llvm::Value *create_term::create_function_call(
910932
alloc_sret = allocate_term(
911933
return_type, current_block_, get_collection_alloc_fn(return_cat.cat),
912934
true);
935+
auto *alloc_sret_cast
936+
= use_gcstrategy
937+
? addrspace_cast0_to0(module_, alloc_sret, current_block_)
938+
: alloc_sret;
913939
sret_type = return_type;
914-
real_args.insert(real_args.begin(), alloc_sret);
915-
types.insert(types.begin(), alloc_sret->getType());
940+
real_args.insert(real_args.begin(), alloc_sret_cast);
941+
types.insert(types.begin(), alloc_sret_cast->getType());
916942
return_type = llvm::Type::getVoidTy(ctx_);
917943
} else if (collection) {
918-
return_type = llvm::PointerType::getUnqual(ctx_);
944+
return_type = use_gcstrategy ? llvm::PointerType::get(ctx_, 0)
945+
: llvm::PointerType::getUnqual(ctx_);
919946
}
920947

921948
llvm::FunctionType *func_type
@@ -1003,15 +1030,20 @@ llvm::Value *create_term::not_injection_case(
10031030
new llvm::StoreInst(child_value, child_ptr, current_block_);
10041031
}
10051032

1006-
auto *block_ptr = llvm::PointerType::getUnqual(module_->getContext());
1033+
auto *block_ptr = use_gcstrategy
1034+
? llvm::PointerType::get(module_->getContext(), 0)
1035+
: llvm::PointerType::getUnqual(module_->getContext());
1036+
auto *block_cast = use_gcstrategy
1037+
? addrspace_cast0_to0(module_, block, current_block_)
1038+
: block;
10071039
if (symbol_decl->attributes().contains(attribute_set::key::Binder)) {
10081040
auto *call = llvm::CallInst::Create(
10091041
get_or_insert_function(module_, "debruijnize", block_ptr, block_ptr),
1010-
block, "withIndices", current_block_);
1042+
block_cast, "withIndices", current_block_);
10111043
set_debug_loc(call);
10121044
return call;
10131045
}
1014-
return block;
1046+
return block_cast;
10151047
}
10161048

10171049
// returns a value and a boolean indicating whether that value could be an
@@ -1190,7 +1222,9 @@ bool make_function(
11901222
std::vector<llvm::Type *> param_types;
11911223
std::vector<std::string> param_names;
11921224
std::vector<llvm::Metadata *> debug_args;
1193-
auto *ptr_ty = llvm::PointerType::getUnqual(module->getContext());
1225+
auto *ptr_ty = use_gcstrategy
1226+
? llvm::PointerType::get(module->getContext(), 0)
1227+
: llvm::PointerType::getUnqual(module->getContext());
11941228
for (auto &entry : vars) {
11951229
auto *sort
11961230
= dynamic_cast<kore_composite_sort *>(entry.second->get_sort().get());
@@ -1359,7 +1393,9 @@ std::string make_apply_rule_function(
13591393
case sort_category::RangeMap:
13601394
case sort_category::List:
13611395
case sort_category::Set:
1362-
param_type = llvm::PointerType::getUnqual(module->getContext());
1396+
param_type = use_gcstrategy
1397+
? llvm::PointerType::get(module->getContext(), 0)
1398+
: llvm::PointerType::getUnqual(module->getContext());
13631399
break;
13641400
default: break;
13651401
}
@@ -1415,8 +1451,11 @@ std::string make_apply_rule_function(
14151451
auto *ptr = allocate_term(
14161452
arg->getType(), creator.get_current_block(),
14171453
get_collection_alloc_fn(cat.cat), true);
1418-
new llvm::StoreInst(arg, ptr, creator.get_current_block());
1419-
arg = ptr;
1454+
auto *ptr_cast = use_gcstrategy ? addrspace_cast0_to0(
1455+
module, ptr, creator.get_current_block())
1456+
: ptr;
1457+
new llvm::StoreInst(arg, ptr_cast, creator.get_current_block());
1458+
arg = ptr_cast;
14201459
}
14211460
break;
14221461
default: break;

runtime/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
22
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
33

44
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/main)
5-
configure_file(main/main.ll ${CMAKE_CURRENT_BINARY_DIR}/main @ONLY)
5+
configure_file(main/main.ll ${CMAKE_CURRENT_BINARY_DIR}/main @ONLY)
6+
7+
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/opaque)
8+
configure_file(opaque/opaque.ll ${CMAKE_CURRENT_BINARY_DIR}/opaque @ONLY)
69

710
install(
811
FILES ${CMAKE_CURRENT_BINARY_DIR}/main/main.ll main/search.cpp
@@ -17,6 +20,7 @@ add_subdirectory(io)
1720
add_subdirectory(json)
1821
add_subdirectory(lto)
1922
add_subdirectory(meta)
23+
add_subdirectory(opaque)
2024
add_subdirectory(strings)
2125
add_subdirectory(util)
2226
add_subdirectory(timer)

runtime/opaque/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/runtime/opaque/opaque_cpp.cpp
2+
COMMAND xxd -i opaque.ll opaque_cpp.cpp
3+
DEPENDS opaque.ll
4+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/runtime/opaque
5+
)
6+
7+
add_library(opaque-cpp STATIC
8+
${CMAKE_BINARY_DIR}/runtime/opaque/opaque_cpp.cpp
9+
)
10+
11+
set_target_properties(opaque-cpp PROPERTIES EXPORT_COMPILE_COMMANDS Off)

runtime/opaque/opaque.ll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target datalayout = "@BACKEND_TARGET_DATALAYOUT@"
2+
target triple = "@BACKEND_TARGET_TRIPLE@"
3+
4+
define ptr addrspace(0) @addrspace_0_to_0(ptr %in) #0 {
5+
; %out = addrspacecast ptr %in to ptr addrspace(0)
6+
; ret ptr addrspace(0) %out
7+
ret ptr addrspace(0) %in
8+
}
9+
10+
attributes #0 = { alwaysinline }

test/defn/imp.kore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// RUN: %interpreter
22
// RUN: %check-grep
33
// RUN: %check-statistics
4-
// RUN: %gcs-interpreter
5-
// RUN: %check-grep
64
// RUN: %proof-interpreter
75
// RUN: %check-proof-out
86
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/test/defn/k-files/imp.md)")]

0 commit comments

Comments
 (0)