[llvm] arm64_32: extend @llvm.stackguard call to in-DAG 64-bits before handing off (PR #77318)

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 8 06:42:44 PST 2024


https://github.com/TNorthover created https://github.com/llvm/llvm-project/pull/77318

arm64_32 pointers are 64-bits in the DAG, so we need to extend the result of loading the cookie when building the DAG.

Most callers of `getLoadStackGuard` feed immediately into a comparison or other use where where the size it already returns is fine, but this one gets put into the canonical value mapping table so it needs fixing to what other nodes are expecting for `ptr` type.

Should also fix some issues in Swift CI.

>From bac534998f7163f91de7dbc9c586dfba19e6d785 Mon Sep 17 00:00:00 2001
From: Tim Northover <tnorthover at apple.com>
Date: Mon, 8 Jan 2024 13:58:52 +0000
Subject: [PATCH] arm64_32: extend @llvm.stackguard call to in-DAG 64-bits
 before handing off

Pointers are 64-bits in the DAG, so we need to extend the result of loading the
cookie when building the DAG.
---
 .../CodeGen/SelectionDAG/SelectionDAGBuilder.cpp   |  4 +++-
 llvm/test/CodeGen/AArch64/arm64_32.ll              | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 1ae682eaf2511c..e772697828f1d7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -6928,11 +6928,13 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
   case Intrinsic::stackguard: {
     MachineFunction &MF = DAG.getMachineFunction();
     const Module &M = *MF.getFunction().getParent();
+    EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
     SDValue Chain = getRoot();
     if (TLI.useLoadStackGuardNode()) {
       Res = getLoadStackGuard(DAG, sdl, Chain);
+      if (Res.getValueType() != PtrTy)
+        Res = DAG.getPtrExtOrTrunc(Res, sdl, PtrTy);
     } else {
-      EVT PtrTy = TLI.getValueType(DAG.getDataLayout(), I.getType());
       const Value *Global = TLI.getSDagStackGuard(M);
       Align Align = DAG.getDataLayout().getPrefTypeAlign(Global->getType());
       Res = DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
diff --git a/llvm/test/CodeGen/AArch64/arm64_32.ll b/llvm/test/CodeGen/AArch64/arm64_32.ll
index 16e2e84a14329d..716fdd6eac15c6 100644
--- a/llvm/test/CodeGen/AArch64/arm64_32.ll
+++ b/llvm/test/CodeGen/AArch64/arm64_32.ll
@@ -759,5 +759,19 @@ define void @test_bzero(i64 %in)  {
 
 declare void @llvm.memset.p0.i32(ptr nocapture writeonly, i8, i32, i1)
 
+define i1 @test_stackguard(ptr %p1) {
+; CHECK-LABEL: test_stackguard:
+; CHECK: adrp x[[TMP:[0-9]+]], ___stack_chk_guard at GOTPAGE
+; CHECK: ldr [[GUARD:w[0-9]+]], [x[[TMP]], ___stack_chk_guard at GOTPAGEOFF]
+; CHECK: cmp [[GUARD]], w
+
+  %p2 = call ptr @llvm.stackguard()
+  %res = icmp ne ptr %p2, %p1
+  ret i1 %res
+}
+declare ptr @llvm.stackguard()
+ at __stack_chk_guard = external global i32
+
+
 !llvm.module.flags = !{!0}
 !0 = !{i32 7, !"PIC Level", i32 2}



More information about the llvm-commits mailing list