Skip to content

Commit aabc24a

Browse files
kamleshbhaluiMaskRay
authored andcommitted
[RISCV] Support llvm.thread.pointer
Fixes https://bugs.llvm.org/show_bug.cgi?id=45303 (clang crashed on __builtin_thread_pointer) Reviewed By: lenary, MaskRay, luismarques Differential Revision: https://reviews.llvm.org/D76828
1 parent b3f6e3d commit aabc24a

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
210210

211211
setOperationAction(ISD::TRAP, MVT::Other, Legal);
212212
setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
213+
setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
213214

214215
if (Subtarget.hasStdExtA()) {
215216
setMaxAtomicSizeInBitsSupported(Subtarget.getXLen());
@@ -429,6 +430,8 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
429430
SDValue FPConv = DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, MVT::f32, NewOp0);
430431
return FPConv;
431432
}
433+
case ISD::INTRINSIC_WO_CHAIN:
434+
return LowerINTRINSIC_WO_CHAIN(Op, DAG);
432435
}
433436
}
434437

@@ -832,6 +835,20 @@ SDValue RISCVTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
832835
return DAG.getMergeValues(Parts, DL);
833836
}
834837

838+
SDValue RISCVTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
839+
SelectionDAG &DAG) const {
840+
unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
841+
SDLoc DL(Op);
842+
switch (IntNo) {
843+
default:
844+
return SDValue(); // Don't custom lower most intrinsics.
845+
case Intrinsic::thread_pointer: {
846+
EVT PtrVT = getPointerTy(DAG.getDataLayout());
847+
return DAG.getRegister(RISCV::X4, PtrVT);
848+
}
849+
}
850+
}
851+
835852
// Returns the opcode of the target-specific SDNode that implements the 32-bit
836853
// form of the given Opcode.
837854
static RISCVISD::NodeType getRISCVWOpcode(unsigned Opcode) {

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class RISCVTargetLowering : public TargetLowering {
203203
SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
204204
SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
205205
SDValue lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, bool IsSRA) const;
206+
SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
206207

207208
bool isEligibleForTailCallOptimization(
208209
CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=riscv64 | FileCheck %s
3+
; RUN: llc < %s -mtriple=riscv32 | FileCheck %s
4+
5+
declare i8* @llvm.thread.pointer()
6+
7+
define i8* @thread_pointer() nounwind {
8+
; CHECK-LABEL: thread_pointer:
9+
; CHECK: # %bb.0:
10+
; CHECK-NEXT: mv a0, tp
11+
; CHECK-NEXT: ret
12+
%1 = tail call i8* @llvm.thread.pointer()
13+
ret i8* %1
14+
}

0 commit comments

Comments
 (0)