Skip to content

Commit 3324462

Browse files
author
Aryan Sefidi
committed
add property to opt in to being a valid vector element type
1 parent 11cbeee commit 3324462

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

llvm/docs/LangRef.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4438,7 +4438,8 @@ the type size is smaller than the type's store size.
44384438
< vscale x <# elements> x <elementtype> > ; Scalable vector
44394439

44404440
The number of elements is a constant integer value larger than 0;
4441-
elementtype may be any integer, floating-point or pointer type. Vectors
4441+
elementtype may be any integer, floating-point, pointer type, or a sized
4442+
target extension type that has the `CanBeVectorElement` property. Vectors
44424443
of size zero are not allowed. For scalable vectors, the total number of
44434444
elements is a constant multiple (called vscale) of the specified number
44444445
of elements; vscale is a positive integer that is unknown at compile time

llvm/include/llvm/IR/DerivedTypes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,8 @@ class TargetExtType : public Type {
845845
/// This type may be allocated on the stack, either as the allocated type
846846
/// of an alloca instruction or as a byval function parameter.
847847
CanBeLocal = 1U << 2,
848+
// This type may be used as an element in a vector.
849+
CanBeVectorElement = 1U << 3,
848850
};
849851

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

llvm/lib/IR/Type.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,13 @@ VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
790790
}
791791

792792
bool VectorType::isValidElementType(Type *ElemTy) {
793-
return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
794-
ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID ||
795-
(ElemTy->isTargetExtTy() && ElemTy->isSized());
793+
if (ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
794+
ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID)
795+
return true;
796+
if (auto *TTy = dyn_cast<TargetExtType>(ElemTy))
797+
return TTy->hasProperty(TargetExtType::CanBeVectorElement) &&
798+
TTy->isSized();
799+
return false;
796800
}
797801

798802
//===----------------------------------------------------------------------===//
@@ -802,8 +806,9 @@ bool VectorType::isValidElementType(Type *ElemTy) {
802806
FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
803807
assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
804808
assert(isValidElementType(ElementType) && "Element type of a VectorType must "
805-
"be an integer, floating point, or "
806-
"pointer type.");
809+
"be an integer, floating point, "
810+
"pointer type, or a valid target "
811+
"extension type.");
807812

808813
auto EC = ElementCount::getFixed(NumElts);
809814

@@ -1038,6 +1043,13 @@ static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
10381043
TargetExtType::CanBeGlobal);
10391044
}
10401045

1046+
// Type used to test vector element target extension property.
1047+
// Can be removed once a public target extension type uses CanBeVectorElement
1048+
if (Name == "llvm.test.vectorelement") {
1049+
return TargetTypeInfo(Type::getInt32Ty(C), TargetExtType::CanBeLocal,
1050+
TargetExtType::CanBeVectorElement);
1051+
}
1052+
10411053
return TargetTypeInfo(Type::getVoidTy(C));
10421054
}
10431055

llvm/test/Verifier/target-ext-vector-invalid.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
; CHECK: invalid vector element type
44

55
define void @bad() {
6-
%v = alloca <2 x target("spirv.IntegralConstant")>
6+
%v = alloca <2 x target("spirv.Image")>
77
ret void
88
}
Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
; RUN: llvm-as -o - %s | llvm-dis | FileCheck %s
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=verify -S %s | FileCheck %s
23

3-
; CHECK-LABEL: @vec_ops(
4-
define <2 x target("spirv.Image")> @vec_ops(<2 x target("spirv.Image")> %x) {
5-
%a = alloca <2 x target("spirv.Image")>
6-
store <2 x target("spirv.Image")> %x, ptr %a
7-
%load = load <2 x target("spirv.Image")>, ptr %a
8-
%elt = extractelement <2 x target("spirv.Image")> %load, i64 0
9-
%res = insertelement <2 x target("spirv.Image")> undef, target("spirv.Image") %elt, i64 1
10-
ret <2 x target("spirv.Image")> %res
11-
}
4+
define <2 x target("llvm.test.vectorelement")> @vec_ops(<2 x target("llvm.test.vectorelement")> %x) {
5+
; CHECK-LABEL: define <2 x target("llvm.test.vectorelement")> @vec_ops(
6+
; CHECK-SAME: <2 x target("llvm.test.vectorelement")> [[X:%.*]]) {
7+
; CHECK-NEXT: [[A:%.*]] = alloca <2 x target("llvm.test.vectorelement")>{{.*}}
8+
; CHECK-NEXT: store <2 x target("llvm.test.vectorelement")> [[X]], ptr [[A]], {{.*}}
9+
; CHECK-NEXT: [[LOAD:%.*]] = load <2 x target("llvm.test.vectorelement")>, ptr [[A]], {{.*}}
10+
; CHECK-NEXT: [[ELT:%.*]] = extractelement <2 x target("llvm.test.vectorelement")> [[LOAD]], i64 0
11+
; CHECK-NEXT: [[RES:%.*]] = insertelement <2 x target("llvm.test.vectorelement")> undef, target("llvm.test.vectorelement") [[ELT]], i64 1
12+
; CHECK-NEXT: ret <2 x target("llvm.test.vectorelement")> [[RES]]
13+
;
14+
%a = alloca <2 x target("llvm.test.vectorelement")>
15+
store <2 x target("llvm.test.vectorelement")> %x, ptr %a
16+
%load = load <2 x target("llvm.test.vectorelement")>, ptr %a
17+
%elt = extractelement <2 x target("llvm.test.vectorelement")> %load, i64 0
18+
%res = insertelement <2 x target("llvm.test.vectorelement")> undef, target("llvm.test.vectorelement") %elt, i64 1
19+
ret <2 x target("llvm.test.vectorelement")> %res
20+
}

0 commit comments

Comments
 (0)