Skip to content

Commit f930497

Browse files
authored
[llvm][NVPTX] Inform that 'DYNAMIC_STACKALLOC' is unsupported (#74684)
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
1 parent 3dde0d0 commit f930497

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/IR/Constants.h"
3838
#include "llvm/IR/DataLayout.h"
3939
#include "llvm/IR/DerivedTypes.h"
40+
#include "llvm/IR/DiagnosticInfo.h"
4041
#include "llvm/IR/FPEnv.h"
4142
#include "llvm/IR/Function.h"
4243
#include "llvm/IR/GlobalValue.h"
@@ -639,6 +640,11 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
639640
setOperationAction(ISD::ConstantFP, MVT::f16, Legal);
640641
setOperationAction(ISD::ConstantFP, MVT::bf16, Legal);
641642

643+
// Lowering of DYNAMIC_STACKALLOC is unsupported.
644+
// Custom lower to produce an error.
645+
setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
646+
setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
647+
642648
// TRAP can be lowered to PTX trap
643649
setOperationAction(ISD::TRAP, MVT::Other, Legal);
644650

@@ -2209,6 +2215,18 @@ SDValue NVPTXTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
22092215
return Chain;
22102216
}
22112217

2218+
SDValue NVPTXTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
2219+
SelectionDAG &DAG) const {
2220+
const Function &Fn = DAG.getMachineFunction().getFunction();
2221+
2222+
DiagnosticInfoUnsupported NoDynamicAlloca(
2223+
Fn, "dynamic alloca unsupported by NVPTX backend",
2224+
SDLoc(Op).getDebugLoc());
2225+
DAG.getContext()->diagnose(NoDynamicAlloca);
2226+
auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
2227+
return DAG.getMergeValues(Ops, SDLoc());
2228+
}
2229+
22122230
// By default CONCAT_VECTORS is lowered by ExpandVectorBuildThroughStack()
22132231
// (see LegalizeDAG.cpp). This is slow and uses local memory.
22142232
// We use extract/insert/build vector just as what LegalizeOp() does in llvm 2.5
@@ -2700,6 +2718,8 @@ NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
27002718
case ISD::SREM:
27012719
case ISD::UREM:
27022720
return LowerVectorArith(Op, DAG);
2721+
case ISD::DYNAMIC_STACKALLOC:
2722+
return LowerDYNAMIC_STACKALLOC(Op, DAG);
27032723
default:
27042724
llvm_unreachable("Custom lowering not defined for operation");
27052725
}

llvm/lib/Target/NVPTX/NVPTXISelLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ class NVPTXTargetLowering : public TargetLowering {
513513
SDValue LowerCall(CallLoweringInfo &CLI,
514514
SmallVectorImpl<SDValue> &InVals) const override;
515515

516+
SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
517+
516518
std::string
517519
getPrototype(const DataLayout &DL, Type *, const ArgListTy &,
518520
const SmallVectorImpl<ISD::OutputArg> &, MaybeAlign retAlignment,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: not llc -march=nvptx < %s 2>&1 | FileCheck %s
2+
; RUN: not llc -march=nvptx64 < %s 2>&1 | FileCheck %s
3+
4+
; CHECK: in function test_dynamic_stackalloc{{.*}}: dynamic alloca unsupported by NVPTX backend
5+
6+
define void @test_dynamic_stackalloc(i64 %n) {
7+
%alloca = alloca i32, i64 %n
8+
store volatile i32 0, ptr %alloca
9+
ret void
10+
}

0 commit comments

Comments
 (0)