Skip to content

Commit c2eda0a

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 8ac140f commit c2eda0a

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
@@ -826,12 +826,14 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
826826
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal);
827827
if (Name.starts_with("spirv."))
828828
return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,
829-
TargetExtType::CanBeGlobal);
829+
TargetExtType::CanBeGlobal,
830+
TargetExtType::CanBeAlloca);
830831

831832
// Opaque types in the AArch64 name space.
832833
if (Name == "aarch64.svcount")
833834
return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),
834-
TargetExtType::HasZeroInit);
835+
TargetExtType::HasZeroInit,
836+
TargetExtType::CanBeAlloca);
835837

836838
return TargetTypeInfo(Type::getVoidTy(C));
837839
}

llvm/lib/IR/Verifier.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4285,6 +4285,12 @@ void Verifier::visitAllocaInst(AllocaInst &AI) {
42854285
SmallPtrSet<Type*, 4> Visited;
42864286
Check(AI.getAllocatedType()->isSized(&Visited),
42874287
"Cannot allocate unsized type", &AI);
4288+
// Check if it's a target extension type that disallows being used in an
4289+
// alloca.
4290+
if (auto *TTy = dyn_cast<TargetExtType>(AI.getAllocatedType())) {
4291+
Check(TTy->hasProperty(TargetExtType::CanBeAlloca),
4292+
"Alloca has illegal target extension type", &AI);
4293+
}
42884294
Check(AI.getArraySize()->getType()->isIntegerTy(),
42894295
"Alloca array size must have integer type", &AI);
42904296
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)