Skip to content

Commit c378384

Browse files
cdisselkoendexonsmith
authored andcommitted
C API: support scalable vectors
This adds support for scalable vector types in the C API and in llvm-c-test, and also adds a test to ensure that llvm-c-test can properly roundtrip operations involving scalable vectors. While creating this diff, I discovered that the C API cannot properly roundtrip _constant expressions_ involving shufflevector / scalable vectors, but that seems to be a separate enough issue that I plan to address it in a future diff (unless reviewers feel it should be addressed here). Differential Revision: https://reviews.llvm.org/D89816
1 parent 90678f6 commit c378384

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

llvm/include/llvm-c/Core.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -1444,9 +1444,21 @@ unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy);
14441444
LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount);
14451445

14461446
/**
1447-
* Obtain the number of elements in a vector type.
1447+
* Create a vector type that contains a defined type and has a scalable
1448+
* number of elements.
1449+
*
1450+
* The created type will exist in the context thats its element type
1451+
* exists in.
1452+
*
1453+
* @see llvm::ScalableVectorType::get()
1454+
*/
1455+
LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
1456+
unsigned ElementCount);
1457+
1458+
/**
1459+
* Obtain the (possibly scalable) number of elements in a vector type.
14481460
*
1449-
* This only works on types that represent vectors.
1461+
* This only works on types that represent vectors (fixed or scalable).
14501462
*
14511463
* @see llvm::VectorType::getNumElements()
14521464
*/

llvm/lib/IR/Core.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,11 @@ LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) {
764764
return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
765765
}
766766

767+
LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
768+
unsigned ElementCount) {
769+
return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
770+
}
771+
767772
LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) {
768773
auto *Ty = unwrap<Type>(WrappedTy);
769774
if (auto *PTy = dyn_cast<PointerType>(Ty))

llvm/test/Bindings/llvm-c/echo.ll

+13
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ define i32 @vectorops(i32, i32) {
174174
ret i32 %p
175175
}
176176

177+
define i32 @scalablevectorops(i32, <vscale x 4 x i32>) {
178+
%a = insertelement <vscale x 4 x i32> undef, i32 %0, i32 0
179+
%b = insertelement <vscale x 4 x i32> %a, i32 %0, i32 2
180+
%c = shufflevector <vscale x 4 x i32> %b, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
181+
%e = add <vscale x 4 x i32> %a, %1
182+
%f = mul <vscale x 4 x i32> %e, %b
183+
%g = xor <vscale x 4 x i32> %f, %e
184+
%h = or <vscale x 4 x i32> %g, %e
185+
%i = lshr <vscale x 4 x i32> %h, undef
186+
%j = extractelement <vscale x 4 x i32> %i, i32 3
187+
ret i32 %j
188+
}
189+
177190
declare void @personalityFn()
178191

179192
define void @exn() personality void ()* @personalityFn {

llvm/tools/llvm-c-test/echo.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ struct TypeCloner {
139139
Clone(LLVMGetElementType(Src)),
140140
LLVMGetPointerAddressSpace(Src)
141141
);
142-
case LLVMScalableVectorTypeKind:
143-
// FIXME: scalable vectors unsupported
144-
break;
145142
case LLVMVectorTypeKind:
146143
return LLVMVectorType(
147144
Clone(LLVMGetElementType(Src)),
148145
LLVMGetVectorSize(Src)
149146
);
147+
case LLVMScalableVectorTypeKind:
148+
return LLVMScalableVectorType(Clone(LLVMGetElementType(Src)),
149+
LLVMGetVectorSize(Src));
150150
case LLVMMetadataTypeKind:
151151
return LLVMMetadataTypeInContext(Ctx);
152152
case LLVMX86_MMXTypeKind:

0 commit comments

Comments
 (0)