Skip to content

Commit c6dfcf2

Browse files
authored
Merge pull request #197 from CodaFi/instruction-locations
Correct the type of debug locations and provide new shims
2 parents dd26f01 + dc3fa9c commit c6dfcf2

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

Sources/LLVM/IRBuilder.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if SWIFT_PACKAGE
22
import cllvm
3+
import llvmshims
34
#endif
45

56
/// An `IRBuilder` is a helper object that generates LLVM instructions. IR
@@ -38,7 +39,7 @@ extension IRBuilder {
3839
/// Repositions the IR Builder before the start of the given instruction.
3940
///
4041
/// - parameter inst: The instruction to reposition the IR Builder before.
41-
public func positionBefore(_ inst: IRValue) {
42+
public func positionBefore(_ inst: IRInstruction) {
4243
LLVMPositionBuilderBefore(llvm, inst.asLLVM())
4344
}
4445

@@ -50,7 +51,7 @@ extension IRBuilder {
5051
///
5152
/// - parameter inst: The instruction to reposition the IR Builder before.
5253
/// - parameter block: The basic block to reposition the IR builder in.
53-
public func position(_ inst: IRValue, block: BasicBlock) {
54+
public func position(_ inst: IRInstruction, block: BasicBlock) {
5455
LLVMPositionBuilder(llvm, block.llvm, inst.asLLVM())
5556
}
5657

@@ -76,7 +77,7 @@ extension IRBuilder {
7677
///
7778
/// - parameter inst: The instruction to insert.
7879
/// - parameter name: The name for the newly inserted instruction.
79-
public func insert(_ inst: IRValue, name: String? = nil) {
80+
public func insert(_ inst: IRInstruction, name: String? = nil) {
8081
if let name = name {
8182
LLVMInsertIntoBuilderWithName(llvm, inst.asLLVM(), name)
8283
} else {
@@ -89,9 +90,9 @@ extension IRBuilder {
8990

9091
extension IRBuilder {
9192
/// Access location information used by debugging information.
92-
public var currentDebugLocation: DebugLocation {
93-
get { return DebugLocation(llvm: LLVMValueAsMetadata(LLVMGetCurrentDebugLocation(self.llvm))) }
94-
set { LLVMSetCurrentDebugLocation(self.llvm, LLVMMetadataAsValue(self.module.context.llvm, newValue.asMetadata())) }
93+
public var currentDebugLocation: DebugLocation? {
94+
get { return LLVMGetCurrentDebugLocation2(self.llvm).map(DebugLocation.init(llvm:)) }
95+
set { LLVMSetCurrentDebugLocation2(self.llvm, newValue?.asMetadata()) }
9596
}
9697
}
9798

Sources/LLVM/Instruction.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if SWIFT_PACKAGE
22
import cllvm
3+
import llvmshims
34
#endif
45

56
/// An `IRInstruction` is a value that directly represents an instruction and
@@ -12,6 +13,12 @@ extension IRInstruction {
1213
return OpCode(rawValue: LLVMGetInstructionOpcode(self.asLLVM()))
1314
}
1415

16+
/// Retrieves the current debug location of this instruction.
17+
public var debugLocation: DebugLocation? {
18+
get { return LLVMInstructionGetDebugLoc(self.asLLVM()).map(DebugLocation.init(llvm:)) }
19+
set { LLVMInstructionSetDebugLoc(self.asLLVM(), newValue?.asMetadata()) }
20+
}
21+
1522
/// Obtain the instruction that occurs before this one, if it exists.
1623
public func previous() -> Instruction? {
1724
guard let val = LLVMGetPreviousInstruction(self.asLLVM()) else { return nil }

Sources/llvmshims/include/shim.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,10 @@ LLVMSymbolIteratorRef LLVMObjectFileGetSymbols(LLVMBinaryRef BR);
7171
LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
7272
LLVMSymbolIteratorRef SI);
7373

74+
LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst);
75+
void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc);
76+
77+
LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder);
78+
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc);
79+
7480
#endif /* LLVMSWIFT_LLVM_SHIM_H */

Sources/llvmshims/src/shim.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "llvm-c/Object.h"
22
#include "llvm/IR/Intrinsics.h"
33
#include "llvm/IR/Function.h"
4+
#include "llvm/IR/IRBuilder.h"
45
#include "llvm/Support/ARMTargetParser.h"
56
#include "llvm/Object/MachOUniversal.h"
67
#include "llvm/Object/ObjectFile.h"
@@ -73,6 +74,14 @@ extern "C" {
7374

7475
LLVMBool LLVMObjectFileIsSymbolIteratorAtEnd(LLVMBinaryRef BR,
7576
LLVMSymbolIteratorRef SI);
77+
78+
// https://reviews.llvm.org/D60481
79+
LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst);
80+
void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc);
81+
82+
// https://reviews.llvm.org/D60484
83+
LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder);
84+
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc);
7685
}
7786

7887
using namespace llvm;
@@ -231,4 +240,24 @@ const char *LLVMGetARMCanonicalArchName(const char *Name, size_t NameLen) {
231240
return llvm::ARM::getCanonicalArchName({Name, NameLen}).data();
232241
}
233242

243+
LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
244+
return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode());
245+
}
246+
247+
void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
248+
if (Loc)
249+
unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc)));
250+
else
251+
unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc());
252+
}
253+
254+
LLVMMetadataRef LLVMGetCurrentDebugLocation2(LLVMBuilderRef Builder) {
255+
return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
256+
}
234257

258+
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Builder, LLVMMetadataRef Loc) {
259+
if (Loc)
260+
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
261+
else
262+
unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
263+
}

Tests/LLVMTests/BFC.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ private func compileProgramBody(
297297
dibuilder.buildDbgValue(of: addressPointer, to: diVariable,
298298
atEndOf: builder.insertBlock!,
299299
expr: dibuilder.buildExpression([.deref]),
300-
location: builder.currentDebugLocation)
300+
location: builder.currentDebugLocation!)
301301
case "<":
302302
// Move left
303303
addressPointer = builder.buildGEP(addressPointer, indices: [ IntType.int32.constant(-1) ])
304304
builder.currentDebugLocation = dibuilder.buildDebugLocation(at: (sourceLine, sourceColumn), in: scope)
305305
dibuilder.buildDbgValue(of: addressPointer, to: diVariable,
306306
atEndOf: builder.insertBlock!,
307307
expr: dibuilder.buildExpression([.deref]),
308-
location: builder.currentDebugLocation)
308+
location: builder.currentDebugLocation!)
309309
case "+":
310310
// Increment
311311
let value = builder.buildLoad(addressPointer)
@@ -315,7 +315,7 @@ private func compileProgramBody(
315315
dibuilder.buildDbgValue(of: value, to: diVariable,
316316
atEndOf: builder.insertBlock!,
317317
expr: dibuilder.buildExpression([.plus_uconst(1)]),
318-
location: builder.currentDebugLocation)
318+
location: builder.currentDebugLocation!)
319319
case "-":
320320
// Decrement
321321
let value = builder.buildLoad(addressPointer)
@@ -325,7 +325,7 @@ private func compileProgramBody(
325325
dibuilder.buildDbgValue(of: value, to: diVariable,
326326
atEndOf: builder.insertBlock!,
327327
expr: dibuilder.buildExpression([.constu(1), .minus]),
328-
location: builder.currentDebugLocation)
328+
location: builder.currentDebugLocation!)
329329
case ".":
330330
// Write
331331
let dataValue = builder.buildLoad(addressPointer)

0 commit comments

Comments
 (0)