Skip to content

Commit 34b21e8

Browse files
committed
[RISCV] Use OS-specific stack-guard ABI for Fuchsia
Fuchsia provides a slot relative to tp for the stack-guard value, which is cheaper to materialize than the default GOT load. Reviewed By: phosek Differential Revision: https://reviews.llvm.org/D143353
1 parent de49093 commit 34b21e8

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14221,6 +14221,25 @@ bool RISCVTargetLowering::preferScalarizeSplat(unsigned Opc) const {
1422114221
return true;
1422214222
}
1422314223

14224+
static Value *useTpOffset(IRBuilderBase &IRB, unsigned Offset) {
14225+
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
14226+
Function *ThreadPointerFunc =
14227+
Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
14228+
return IRB.CreatePointerCast(
14229+
IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
14230+
IRB.CreateCall(ThreadPointerFunc), Offset),
14231+
IRB.getInt8PtrTy()->getPointerTo(0));
14232+
}
14233+
14234+
Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
14235+
// Fuchsia provides a fixed TLS slot for the stack cookie.
14236+
// <zircon/tls.h> defines ZX_TLS_STACK_GUARD_OFFSET with this value.
14237+
if (Subtarget.isTargetFuchsia())
14238+
return useTpOffset(IRB, -0x10);
14239+
14240+
return TargetLowering::getIRStackGuard(IRB);
14241+
}
14242+
1422414243
#define GET_REGISTER_MATCHER
1422514244
#include "RISCVGenAsmMatcher.inc"
1422614245

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ class RISCVTargetLowering : public TargetLowering {
633633
return Scale == 1;
634634
}
635635

636+
/// If the target has a standard location for the stack protector cookie,
637+
/// returns the address of that location. Otherwise, returns nullptr.
638+
Value *getIRStackGuard(IRBuilderBase &IRB) const override;
639+
636640
private:
637641
/// RISCVCCAssignFn - This target-specific function extends the default
638642
/// CCValAssign with additional information used to lower RISC-V calling

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
176176
const LegalizerInfo *getLegalizerInfo() const override;
177177
const RegisterBankInfo *getRegBankInfo() const override;
178178

179+
bool isTargetFuchsia() const { return getTargetTriple().isOSFuchsia(); }
180+
179181
bool useConstantPoolForLargeInts() const;
180182

181183
// Maximum cost used for building integers, integers will be put into constant
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
3+
;; Test target-specific stack cookie location.
4+
;
5+
; RUN: llc -mtriple=riscv64-fuchsia < %s | FileCheck --check-prefix=FUCHSIA-RISCV64 %s
6+
7+
define void @func() sspreq {
8+
; FUCHSIA-RISCV64-LABEL: func:
9+
; FUCHSIA-RISCV64: # %bb.0:
10+
; FUCHSIA-RISCV64-NEXT: addi sp, sp, -32
11+
; FUCHSIA-RISCV64-NEXT: .cfi_def_cfa_offset 32
12+
; FUCHSIA-RISCV64-NEXT: sd ra, 24(sp) # 8-byte Folded Spill
13+
; FUCHSIA-RISCV64-NEXT: .cfi_offset ra, -8
14+
; FUCHSIA-RISCV64-NEXT: ld a0, -16(tp)
15+
; FUCHSIA-RISCV64-NEXT: sd a0, 16(sp)
16+
; FUCHSIA-RISCV64-NEXT: addi a0, sp, 12
17+
; FUCHSIA-RISCV64-NEXT: call capture@plt
18+
; FUCHSIA-RISCV64-NEXT: ld a0, -16(tp)
19+
; FUCHSIA-RISCV64-NEXT: ld a1, 16(sp)
20+
; FUCHSIA-RISCV64-NEXT: bne a0, a1, .LBB0_2
21+
; FUCHSIA-RISCV64-NEXT: # %bb.1: # %SP_return
22+
; FUCHSIA-RISCV64-NEXT: ld ra, 24(sp) # 8-byte Folded Reload
23+
; FUCHSIA-RISCV64-NEXT: addi sp, sp, 32
24+
; FUCHSIA-RISCV64-NEXT: ret
25+
; FUCHSIA-RISCV64-NEXT: .LBB0_2: # %CallStackCheckFailBlk
26+
; FUCHSIA-RISCV64-NEXT: call __stack_chk_fail@plt
27+
%1 = alloca i32, align 4
28+
call void @capture(ptr nonnull %1)
29+
ret void
30+
}
31+
32+
declare void @capture(ptr)

0 commit comments

Comments
 (0)