Skip to content

Commit e595e03

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 79eb970 + 8de42ea commit e595e03

File tree

11 files changed

+126
-0
lines changed

11 files changed

+126
-0
lines changed

bin/llvm-kompile

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Options:
4242
(immutable) that are enabled by default.
4343
--hidden-visibility Set the visibility of all global symbols in generated code to
4444
"hidden"
45+
--use-gcstrategy Use GC strategy defined for the LLVM backend.
4546
--profile-matching Instrument interpeter to emit a profile of time spent in
4647
top-level rule matching on stderr.
4748
--verify-ir Verify result of IR generation.
@@ -197,6 +198,11 @@ while [[ $# -gt 0 ]]; do
197198
kompile_clang_flags+=("--hidden-visibility")
198199
shift
199200
;;
201+
--use-gcstrategy)
202+
codegen_flags+=("--use-gcstrategy")
203+
kompile_clang_flags+=("--use-gcstrategy")
204+
shift
205+
;;
200206
--profile-matching)
201207
codegen_flags+=("--profile-matching")
202208
codegen_verify_flags+=("--profile-matching")

bin/llvm-kompile-clang

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ flags=()
1515
llc_flags=()
1616
llc_opt_flags="-O0"
1717
visibility_hidden=false
18+
use_gcstrategy=false
1819
link=true
1920
export verbose=false
2021
export profile=false
@@ -101,6 +102,10 @@ while [[ $# -gt 0 ]]; do
101102
visibility_hidden=true
102103
shift
103104
;;
105+
--use-gcstrategy)
106+
use_gcstrategy=true
107+
shift
108+
;;
104109
*)
105110
;;
106111
esac
@@ -188,6 +193,9 @@ if [ "$main" != "python_ast" ]; then
188193
run @OPT@ "$modopt" -load-pass-plugin "$passes" -set-visibility-hidden -o "$modhidden"
189194
modopt="$modhidden"
190195
fi
196+
if $use_gcstrategy; then
197+
llc_flags+=("-load="$passes"")
198+
fi
191199
run @LLC@ \
192200
"$modopt" -mtriple=@BACKEND_TARGET_TRIPLE@ \
193201
-filetype=obj "$llc_opt_flags" "${llc_flags[@]}" -o "$modasm"

include/kllvm/codegen/GCStrategy.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- Extend GCStrategy of llvm/CodeGen/GCStrategy.h ---------------------===//
2+
//
3+
// We extend the base GCStrategy as follows:
4+
// - use gc.safepoints instead of (default) gc.roots.
5+
// - specify that the RewriteStatepointsForGC pass should rewrite the calls of
6+
// this function.
7+
// - pointers with address space != 0 are pointing to GC-managed memory.
8+
//===----------------------------------------------------------------------===//
9+
10+
// NOLINTBEGIN
11+
12+
#ifndef LLVM_BACKEND_GC_STRATEGY_H
13+
#define LLVM_BACKEND_GC_STRATEGY_H
14+
15+
#include "llvm/IR/GCStrategy.h"
16+
#include "llvm/IR/Type.h"
17+
18+
namespace kllvm {
19+
20+
/// The GCStrategy for the LLVM Backend
21+
class LLVMBackendGCStrategy : public llvm::GCStrategy {
22+
public:
23+
LLVMBackendGCStrategy();
24+
25+
// Override
26+
#if LLVM_VERSION_MAJOR == 15
27+
llvm::Optional<bool> isGCManagedPointer(llvm::Type const *Ty) const override;
28+
#else
29+
std::optional<bool> isGCManagedPointer(llvm::Type const *Ty) const override;
30+
#endif
31+
};
32+
33+
} // namespace kllvm
34+
35+
#endif // LLVM_BACKEND_GC_STRATEGY_H
36+
37+
// NOLINTEND

include/kllvm/codegen/Options.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern llvm::cl::opt<bool> no_optimize;
1010
extern llvm::cl::opt<bool> emit_object;
1111
extern llvm::cl::opt<bool> binary_ir;
1212
extern llvm::cl::opt<bool> force_binary;
13+
extern llvm::cl::opt<bool> use_gcstrategy;
1314
extern llvm::cl::opt<bool> proof_hint_instrumentation;
1415
extern llvm::cl::opt<bool> proof_hint_instrumentation_slow;
1516
extern llvm::cl::opt<bool> keep_frame_pointer;

lib/codegen/CreateTerm.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "kllvm/codegen/CreateTerm.h"
22
#include "kllvm/codegen/CreateStaticTerm.h"
33
#include "kllvm/codegen/Debug.h"
4+
#include "kllvm/codegen/Options.h"
45
#include "kllvm/codegen/ProofEvent.h"
56
#include "kllvm/codegen/Util.h"
67

@@ -1224,6 +1225,9 @@ bool make_function(
12241225
= llvm::FunctionType::get(return_type, param_types, false);
12251226
llvm::Function *apply_rule = get_or_insert_function(module, name, func_type);
12261227
apply_rule->setLinkage(llvm::GlobalValue::InternalLinkage);
1228+
if (use_gcstrategy) {
1229+
apply_rule->setGC("gcs-llvm-backend");
1230+
}
12271231
init_debug_axiom(axiom->attributes());
12281232
std::string debug_name = name;
12291233
if (axiom->attributes().contains(attribute_set::key::Label)) {

lib/codegen/Options.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ cl::opt<bool> force_binary(
4848
"f", cl::desc("Force binary bitcode output to stdout"), cl::Hidden,
4949
cl::cat(codegen_lib_cat));
5050

51+
cl::opt<bool> use_gcstrategy(
52+
"use-gcstrategy", cl::desc("Use GC strategy defined for the LLVM backend."),
53+
cl::Hidden, cl::init(false), cl::cat(codegen_lib_cat));
54+
5155
namespace kllvm {
5256

5357
void validate_codegen_args(bool is_tty) {

lib/passes/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ add_library(KLLVMPassInternal
22
SetVisibilityHidden.cpp
33
RemoveDeadKFunctions.cpp
44
MustTailDeadArgElimination.cpp
5+
GCStrategy.cpp
56
PluginInfo.cpp
67
)
78

89
add_library(KLLVMPass MODULE
910
SetVisibilityHidden.cpp
1011
RemoveDeadKFunctions.cpp
1112
MustTailDeadArgElimination.cpp
13+
GCStrategy.cpp
1214
PluginInfo.cpp
1315
)
1416

lib/passes/GCStrategy.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===- Extend GCStrategy of llvm/CodeGen/GCStrategy.h ---------------------===//
2+
//
3+
// We extend the base GCStrategy as follows:
4+
// - use gc.safepoints instead of (default) gc.roots.
5+
// - specify that the RewriteStatepointsForGC pass should rewrite the calls of
6+
// this function.
7+
// - pointers with address space != 0 are pointing to GC-managed memory.
8+
//===----------------------------------------------------------------------===//
9+
10+
// NOLINTBEGIN
11+
12+
#include "kllvm/codegen/GCStrategy.h"
13+
14+
#include "llvm/CodeGen/GCMetadata.h"
15+
#include "llvm/IR/DerivedTypes.h"
16+
#include "llvm/Support/Compiler.h"
17+
18+
using namespace llvm;
19+
using namespace kllvm;
20+
21+
LLVMBackendGCStrategy::LLVMBackendGCStrategy() {
22+
UseStatepoints = true; // Use gc.statepoints
23+
#if LLVM_VERSION_MAJOR != 15
24+
UseRS4GC = true; // Rewrite the calls of a function that has this GCStrategy
25+
#endif
26+
}
27+
28+
// Override
29+
#if LLVM_VERSION_MAJOR == 15
30+
llvm::Optional<bool>
31+
LLVMBackendGCStrategy::isGCManagedPointer(Type const *Ty) const {
32+
#else
33+
std::optional<bool>
34+
LLVMBackendGCStrategy::isGCManagedPointer(Type const *Ty) const {
35+
#endif
36+
// Return false for any non-pointer type
37+
if (!Ty->isPointerTy()) {
38+
return false;
39+
}
40+
// Any pointer with address space != 0 is to managed memory.
41+
PointerType const *PTy = dyn_cast<PointerType>(Ty);
42+
if (PTy->getAddressSpace()) {
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
// Add LLVMBackendGCStrategy to the global GCRegistry
49+
static GCRegistry::Add<LLVMBackendGCStrategy>
50+
X("gcs-llvm-backend", "GC Strategy for the LLVM Backend");
51+
52+
// NOLINTEND

test/defn/imp.kore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %interpreter
22
// RUN: %check-grep
33
// RUN: %check-statistics
4+
// RUN: %gcs-interpreter
5+
// RUN: %check-grep
46
// RUN: %proof-interpreter
57
// RUN: %check-proof-out
68
[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)")]

test/lit.cfg.py

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ def exclude_x86_and_llvm18(s):
9898
exit 1
9999
fi
100100
''')),
101+
('%gcs-interpreter', one_line('''
102+
output=$(%kompile %s main --use-gcstrategy -o %t.interpreter 2>&1)
103+
if [[ -n "$output" ]]; then
104+
echo "llvm-kompile error or warning: $output"
105+
exit 1
106+
fi
107+
''')),
101108
('%proof-interpreter', one_line('''
102109
output=$(%kompile %s main --proof-hint-instrumentation -o %t.interpreter 2>&1)
103110
if [[ -n "$output" ]]; then

tools/llvm-kompile-codegen/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "kllvm/codegen/GCStrategy.h"
12
#include <kllvm/ast/AST.h>
23
#include <kllvm/codegen/ApplyPasses.h>
34
#include <kllvm/codegen/CreateTerm.h>
@@ -147,6 +148,8 @@ void emit_metadata(llvm::Module &mod) {
147148

148149
// NOLINTNEXTLINE(*-cognitive-complexity)
149150
int main(int argc, char **argv) {
151+
// NOLINTNEXTLINE(*-identifier-naming)
152+
LLVMBackendGCStrategy _gcs; // Unused. This is needed to ensure linking.
150153
initialize_llvm();
151154

152155
cl::HideUnrelatedOptions({&codegen_tool_cat, &codegen_lib_cat});

0 commit comments

Comments
 (0)