[llvm] [llvm][NVPTX] Inform that 'DYNAMIC_STACKALLOC' is unsupported (PR #74684)
Youngsuk Kim via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 14 09:45:59 PST 2023
https://github.com/JOE1994 updated https://github.com/llvm/llvm-project/pull/74684
>From f176341c676c3c2bd75dcf1c2afc4ce78d7c75da Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Wed, 6 Dec 2023 18:38:00 -0600
Subject: [PATCH 1/2] [llvm][NVPTX] Inform that 'DYNAMIC_STACKALLOC' is
unsupported
Catch unsupported path early up, and emit error with information.
Motivated by the following threads:
* https://discourse.llvm.org/t/nvptx-problems-with-dynamic-alloca/70745
* #64017
---
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index 61285c6ba98dff..b62072dc796e7a 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -639,6 +639,11 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
setOperationAction(ISD::ConstantFP, MVT::f16, Legal);
setOperationAction(ISD::ConstantFP, MVT::bf16, Legal);
+ // Lowering of DYNAMIC_STACKALLOC is unsupported.
+ // Custom lower to produce an error.
+ setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
+ setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
+
// TRAP can be lowered to PTX trap
setOperationAction(ISD::TRAP, MVT::Other, Legal);
@@ -2652,6 +2657,9 @@ NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
case ISD::SREM:
case ISD::UREM:
return LowerVectorArith(Op, DAG);
+ case ISD::DYNAMIC_STACKALLOC:
+ report_fatal_error(
+ "Dynamic stack allocation is yet unsupported by NVPTX backend.");
default:
llvm_unreachable("Custom lowering not defined for operation");
}
>From 7ae0afa347b402bb6be9b33fb80a5c338b991647 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Thu, 14 Dec 2023 11:41:44 -0600
Subject: [PATCH 2/2] Use DiagnosticInfo as per feedback from @inclyc
---
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp | 16 ++++++++++++++--
llvm/lib/Target/NVPTX/NVPTXISelLowering.h | 2 ++
llvm/test/CodeGen/NVPTX/dynamic_stackalloc.ll | 10 ++++++++++
3 files changed, 26 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/NVPTX/dynamic_stackalloc.ll
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index b62072dc796e7a..3c1faaa6be522d 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -37,6 +37,7 @@
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/FPEnv.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
@@ -2203,6 +2204,18 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
return Chain;
}
+SDValue NVPTXTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
+ SelectionDAG &DAG) const {
+ const Function &Fn = DAG.getMachineFunction().getFunction();
+
+ DiagnosticInfoUnsupported NoDynamicAlloca(
+ Fn, "dynamic alloca unsupported by NVPTX backend",
+ SDLoc(Op).getDebugLoc());
+ DAG.getContext()->diagnose(NoDynamicAlloca);
+ auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
+ return DAG.getMergeValues(Ops, SDLoc());
+}
+
// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
// (see LegalizeDAG.cpp). This is slow and uses local memory.
// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
@@ -2658,8 +2671,7 @@ NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
case ISD::UREM:
return LowerVectorArith(Op, DAG);
case ISD::DYNAMIC_STACKALLOC:
- report_fatal_error(
- "Dynamic stack allocation is yet unsupported by NVPTX backend.");
+ return LowerDYNAMIC_STACKALLOC(Op, DAG);
default:
llvm_unreachable("Custom lowering not defined for operation");
}
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
index 54e34dedc6675e..020cc8130eee4f 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.h
@@ -513,6 +513,8 @@ class NVPTXTargetLowering : public TargetLowering {
SDValue LowerCall(CallLoweringInfo &CLI,
SmallVectorImpl<SDValue> &InVals) const override;
+ SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
+
std::string
getPrototype(const DataLayout &DL, Type *, const ArgListTy &,
const SmallVectorImpl<ISD::OutputArg> &, MaybeAlign retAlignment,
diff --git a/llvm/test/CodeGen/NVPTX/dynamic_stackalloc.ll b/llvm/test/CodeGen/NVPTX/dynamic_stackalloc.ll
new file mode 100644
index 00000000000000..3ef55ca5309f88
--- /dev/null
+++ b/llvm/test/CodeGen/NVPTX/dynamic_stackalloc.ll
@@ -0,0 +1,10 @@
+; RUN: not llc -march=nvptx < %s 2>&1 | FileCheck %s
+; RUN: not llc -march=nvptx64 < %s 2>&1 | FileCheck %s
+
+; CHECK: in function test_dynamic_stackalloc{{.*}}: dynamic alloca unsupported by NVPTX backend
+
+define void @test_dynamic_stackalloc(i64 %n) {
+ %alloca = alloca i32, i64 %n
+ store volatile i32 0, ptr %alloca
+ ret void
+}
More information about the llvm-commits
mailing list