Skip to content

Commit 4aeea63

Browse files
committed
[IR] Add TargetExtType::CanBeAlloca property
Add a property to allow marking target extension types that cannot be used in an alloca instruction, similar to CanBeGlobal for global variables.
1 parent f091848 commit 4aeea63

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

llvm/include/llvm/IR/DerivedTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ class TargetExtType : public Type {
769769
HasZeroInit = 1U << 0,
770770
/// This type may be used as the value type of a global variable.
771771
CanBeGlobal = 1U << 1,
772+
/// This type may be used as the allocated type of an alloca instruction.
773+
CanBeAlloca = 1U << 2,
772774
};
773775

774776
/// Returns true if the target extension type contains the given property.

llvm/lib/IR/Type.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,12 +838,14 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
838838
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal);
839839
if (Name.starts_with("spirv."))
840840
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,
841-
TargetExtType::CanBeGlobal);
841+
TargetExtType::CanBeGlobal,
842+
TargetExtType::CanBeAlloca);
842843

843844
// Opaque types in the AArch64 name space.
844845
if (Name == "aarch64.svcount")
845846
return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),
846-
TargetExtType::HasZeroInit);
847+
TargetExtType::HasZeroInit,
848+
TargetExtType::CanBeAlloca);
847849

848850
return TargetTypeInfo(Type::getVoidTy(C));
849851
}

llvm/lib/IR/Verifier.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,6 +4273,12 @@ void Verifier::visitAllocaInst(AllocaInst &AI) {
42734273
SmallPtrSet<Type*, 4> Visited;
42744274
Check(AI.getAllocatedType()->isSized(&Visited),
42754275
"Cannot allocate unsized type", &AI);
4276+
// Check if it's a target extension type that disallows being used in an
4277+
// alloca.
4278+
if (auto *TTy = dyn_cast<TargetExtType>(AI.getAllocatedType())) {
4279+
Check(TTy->hasProperty(TargetExtType::CanBeAlloca),
4280+
"Alloca has illegal target extension type", &AI);
4281+
}
42764282
Check(AI.getArraySize()->getType()->isIntegerTy(),
42774283
"Alloca array size must have integer type", &AI);
42784284
if (MaybeAlign A = AI.getAlign()) {

llvm/test/Assembler/target-type-properties.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; RUN: split-file %s %t
22
; RUN: not llvm-as < %t/zeroinit-error.ll -o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-ZEROINIT %s
33
; RUN: not llvm-as < %t/global-var.ll -o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-GLOBALVAR %s
4+
; RUN: not llvm-as < %t/alloca.ll -o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-ALLOCA %s
45
; Check target extension type properties are verified in the assembler.
56

67
;--- zeroinit-error.ll
@@ -14,3 +15,10 @@ define void @foo() {
1415
;--- global-var.ll
1516
@global = external global target("unknown_target_type")
1617
; CHECK-GLOBALVAR: Global @global has illegal target extension type
18+
19+
;--- alloca.ll
20+
define void @foo() {
21+
%val = alloca target("spirv.Image")
22+
; CHECK-ALLOCA: Alloca has illegal target extension type
23+
ret void
24+
}

0 commit comments

Comments
 (0)