Skip to content

Commit 3a83996

Browse files
authored
Remove LLVMExtSetCurrentDebugLocation from llvm_ext.cc for LLVM 9+ (#13965)
1 parent 2543c09 commit 3a83996

File tree

8 files changed

+53
-44
lines changed

8 files changed

+53
-44
lines changed

src/compiler/crystal/codegen/crystal_llvm_builder.cr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ module Crystal
7474
end
7575

7676
{% for name in %w(add add_handler alloca and ashr atomicrmw bit_cast build_catch_ret call
77-
catch_pad catch_switch cmpxchg cond current_debug_location exact_sdiv
78-
extract_value fadd fcmp fdiv fence fmul fp2si fp2ui fpext fptrunc fsub
79-
global_string_pointer icmp inbounds_gep int2ptr invoke landing_pad load
80-
lshr mul not or phi ptr2int sdiv select set_current_debug_location sext
81-
shl si2fp srem store store_volatile sub switch trunc udiv ui2fp urem va_arg
82-
xor zext) %}
77+
catch_pad catch_switch clear_current_debug_location cmpxchg cond
78+
current_debug_location exact_sdiv extract_value fadd fcmp fdiv fence fmul
79+
fp2si fp2ui fpext fptrunc fsub global_string_pointer icmp inbounds_gep int2ptr
80+
invoke landing_pad load lshr mul not or phi ptr2int sdiv select
81+
set_current_debug_location sext shl si2fp srem store store_volatile sub switch
82+
trunc udiv ui2fp urem va_arg xor zext) %}
8383
def {{name.id}}(*args, **kwargs)
8484
return llvm_nil if @end
8585

src/compiler/crystal/codegen/debug.cr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ module Crystal
463463
scope = get_current_debug_scope(location)
464464

465465
if scope
466-
builder.set_current_debug_location(location.line_number || 1, location.column_number, scope)
466+
debug_loc = di_builder.create_debug_location(location.line_number || 1, location.column_number, scope)
467+
# NOTE: `di_builder.context` is only necessary for LLVM 8
468+
builder.set_current_debug_location(debug_loc, di_builder.context)
467469
else
468470
clear_current_debug_location
469471
end
@@ -472,7 +474,7 @@ module Crystal
472474
def clear_current_debug_location
473475
@current_debug_location = nil
474476

475-
builder.set_current_debug_location(0, 0, nil)
477+
builder.clear_current_debug_location
476478
end
477479

478480
def emit_fun_debug_metadata(func, fun_name, location, *, debug_types = [] of LibLLVM::MetadataRef, is_optimized = false)

src/llvm/builder.cr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,27 @@ class LLVM::Builder
333333
Value.new LibLLVM.build_va_arg(self, list, type, name)
334334
end
335335

336+
@[Deprecated("Call `#set_current_debug_location(metadata, context)` or `#clear_current_debug_location` instead")]
336337
def set_current_debug_location(line, column, scope, inlined_at = nil)
337338
LibLLVMExt.set_current_debug_location(self, line, column, scope, inlined_at)
338339
end
339340

341+
def set_current_debug_location(metadata, context : LLVM::Context)
342+
{% if LibLLVM::IS_LT_90 %}
343+
LibLLVM.set_current_debug_location(self, LibLLVM.metadata_as_value(context, metadata))
344+
{% else %}
345+
LibLLVM.set_current_debug_location2(self, metadata)
346+
{% end %}
347+
end
348+
349+
def clear_current_debug_location
350+
{% if LibLLVM::IS_LT_90 %}
351+
LibLLVMExt.clear_current_debug_location(self)
352+
{% else %}
353+
LibLLVM.set_current_debug_location2(self, nil)
354+
{% end %}
355+
end
356+
340357
def set_metadata(value, kind, node)
341358
LibLLVM.set_metadata(value, kind, node)
342359
end

src/llvm/di_builder.cr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ struct LLVM::DIBuilder
1414
LibLLVM.dispose_di_builder(self)
1515
end
1616

17+
def context
18+
@llvm_module.context
19+
end
20+
1721
def create_compile_unit(lang : DwarfSourceLanguage, file, dir, producer, optimized, flags, runtime_version)
1822
file = create_file(file, dir)
1923
{% if LibLLVM::IS_LT_110 %}
@@ -151,6 +155,10 @@ struct LLVM::DIBuilder
151155
LibLLVM.di_builder_get_or_create_subrange(self, lo, count)
152156
end
153157

158+
def create_debug_location(line, column, scope, inlined_at = nil)
159+
LibLLVM.di_builder_create_debug_location(context, line, column, scope, inlined_at)
160+
end
161+
154162
def end
155163
LibLLVM.di_builder_finalize(self)
156164
end
@@ -191,7 +199,7 @@ struct LLVM::DIBuilder
191199
end
192200

193201
private def extract_metadata_array(metadata : LibLLVM::MetadataRef)
194-
metadata_as_value = LibLLVM.metadata_as_value(@llvm_module.context, metadata)
202+
metadata_as_value = LibLLVM.metadata_as_value(context, metadata)
195203
operand_count = LibLLVM.get_md_node_num_operands(metadata_as_value).to_i
196204
operands = Pointer(LibLLVM::ValueRef).malloc(operand_count)
197205
LibLLVM.get_md_node_operands(metadata_as_value, operands)

src/llvm/ext/llvm_ext.cc

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#include <llvm/IR/DIBuilder.h>
1+
#include <llvm/Config/llvm-config.h>
22
#include <llvm/IR/IRBuilder.h>
3-
#include <llvm/IR/DebugLoc.h>
43
#include <llvm/Target/TargetMachine.h>
54
#include <llvm-c/TargetMachine.h>
65

@@ -9,50 +8,27 @@ using namespace llvm;
98
#define LLVM_VERSION_GE(major, minor) \
109
(LLVM_VERSION_MAJOR > (major) || LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR >= (minor))
1110

12-
#define LLVM_VERSION_EQ(major, minor) \
13-
(LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR == (minor))
14-
15-
#define LLVM_VERSION_LE(major, minor) \
16-
(LLVM_VERSION_MAJOR < (major) || LLVM_VERSION_MAJOR == (major) && LLVM_VERSION_MINOR <= (minor))
11+
#if !LLVM_VERSION_GE(9, 0)
12+
#include <llvm/IR/DIBuilder.h>
13+
#endif
1714

1815
#if LLVM_VERSION_GE(16, 0)
1916
#define makeArrayRef ArrayRef
2017
#endif
2118

22-
typedef DIBuilder *DIBuilderRef;
23-
#define DIArray DINodeArray
24-
template <typename T> T *unwrapDIptr(LLVMMetadataRef v) {
25-
return (T *)(v ? unwrap<MDNode>(v) : NULL);
26-
}
27-
2819
extern "C" {
2920

30-
#if LLVM_VERSION_GE(9, 0)
31-
#else
21+
#if !LLVM_VERSION_GE(9, 0)
3222
LLVMMetadataRef LLVMExtDIBuilderCreateEnumerator(
3323
LLVMDIBuilderRef Dref, const char *Name, int64_t Value) {
3424
DIEnumerator *e = unwrap(Dref)->createEnumerator(Name, Value);
3525
return wrap(e);
3626
}
37-
#endif
3827

39-
void LLVMExtSetCurrentDebugLocation(
40-
LLVMBuilderRef Bref, unsigned Line, unsigned Col, LLVMMetadataRef Scope,
41-
LLVMMetadataRef InlinedAt) {
42-
#if LLVM_VERSION_GE(12, 0)
43-
if (!Scope)
44-
unwrap(Bref)->SetCurrentDebugLocation(DebugLoc());
45-
else
46-
unwrap(Bref)->SetCurrentDebugLocation(
47-
DILocation::get(unwrap<MDNode>(Scope)->getContext(), Line, Col,
48-
unwrapDIptr<DILocalScope>(Scope),
49-
unwrapDIptr<DILocation>(InlinedAt)));
50-
#else
51-
unwrap(Bref)->SetCurrentDebugLocation(
52-
DebugLoc::get(Line, Col, Scope ? unwrap<MDNode>(Scope) : nullptr,
53-
InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
54-
#endif
28+
void LLVMExtClearCurrentDebugLocation(LLVMBuilderRef B) {
29+
unwrap(B)->SetCurrentDebugLocation(DebugLoc::get(0, 0, nullptr));
5530
}
31+
#endif
5632

5733
OperandBundleDef *LLVMExtBuildOperandBundleDef(
5834
const char *Name, LLVMValueRef *Inputs, unsigned NumInputs) {

src/llvm/lib_llvm/core.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,11 @@ lib LibLLVM
183183
fun get_insert_block = LLVMGetInsertBlock(builder : BuilderRef) : BasicBlockRef
184184
fun dispose_builder = LLVMDisposeBuilder(builder : BuilderRef)
185185

186-
{% unless LibLLVM::IS_LT_90 %}
186+
{% if LibLLVM::IS_LT_90 %}
187+
fun set_current_debug_location = LLVMSetCurrentDebugLocation(builder : BuilderRef, l : ValueRef)
188+
{% else %}
187189
fun get_current_debug_location2 = LLVMGetCurrentDebugLocation2(builder : BuilderRef) : MetadataRef
190+
fun set_current_debug_location2 = LLVMSetCurrentDebugLocation2(builder : BuilderRef, loc : MetadataRef)
188191
{% end %}
189192
fun get_current_debug_location = LLVMGetCurrentDebugLocation(builder : BuilderRef) : ValueRef
190193

src/llvm/lib_llvm/debug_info.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ lib LibLLVM
4545
builder : DIBuilderRef, scope : MetadataRef, file_scope : MetadataRef, discriminator : UInt
4646
) : MetadataRef
4747

48+
fun di_builder_create_debug_location = LLVMDIBuilderCreateDebugLocation(
49+
ctx : ContextRef, line : UInt, column : UInt, scope : MetadataRef, inlined_at : MetadataRef
50+
) : MetadataRef
51+
4852
fun di_builder_get_or_create_type_array = LLVMDIBuilderGetOrCreateTypeArray(builder : DIBuilderRef, types : MetadataRef*, length : SizeT) : MetadataRef
4953

5054
fun di_builder_create_subroutine_type = LLVMDIBuilderCreateSubroutineType(

src/llvm/lib_llvm_ext.cr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ lib LibLLVMExt
1313

1414
{% if LibLLVM::IS_LT_90 %}
1515
fun di_builder_create_enumerator = LLVMExtDIBuilderCreateEnumerator(builder : LibLLVM::DIBuilderRef, name : Char*, value : Int64) : LibLLVM::MetadataRef
16+
fun clear_current_debug_location = LLVMExtClearCurrentDebugLocation(b : LibLLVM::BuilderRef)
1617
{% end %}
1718

18-
fun set_current_debug_location = LLVMExtSetCurrentDebugLocation(LibLLVM::BuilderRef, Int, Int, LibLLVM::MetadataRef, LibLLVM::MetadataRef)
19-
2019
fun build_operand_bundle_def = LLVMExtBuildOperandBundleDef(name : LibC::Char*,
2120
input : LibLLVM::ValueRef*,
2221
num_input : LibC::UInt) : LibLLVMExt::OperandBundleDefRef

0 commit comments

Comments
 (0)