Skip to content

Commit 35276f1

Browse files
nektronikic
authored andcommitted
[llvm-c] Add C API methods to match 64bit ArrayType C++ API signatures
Fixes #56496. As mentioned in the issue, new functions LLVMArrayType2 and LLVMGetArrayLength2 are created so as to not break the old API. The old methods are then marked as deprecated and callers are updated. Differential Revision: https://reviews.llvm.org/D143700
1 parent 7171244 commit 35276f1

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

llvm/bindings/ocaml/llvm/llvm_ocaml.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ LLVMTypeRef llvm_vector_type(LLVMTypeRef ElementTy, value Count) {
587587

588588
/* lltype -> int */
589589
value llvm_array_length(LLVMTypeRef ArrayTy) {
590-
return Val_int(LLVMGetArrayLength(ArrayTy));
590+
return Val_int(LLVMGetArrayLength2(ArrayTy));
591591
}
592592

593593
/* lltype -> int */

llvm/include/llvm-c/Core.h

+33
Original file line numberDiff line numberDiff line change
@@ -1437,19 +1437,42 @@ unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp);
14371437
* The created type will exist in the context that its element type
14381438
* exists in.
14391439
*
1440+
* @deprecated LLVMArrayType is deprecated in favor of the API accurate
1441+
* LLVMArrayType2
14401442
* @see llvm::ArrayType::get()
14411443
*/
14421444
LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount);
14431445

1446+
/**
1447+
* Create a fixed size array type that refers to a specific type.
1448+
*
1449+
* The created type will exist in the context that its element type
1450+
* exists in.
1451+
*
1452+
* @see llvm::ArrayType::get()
1453+
*/
1454+
LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount);
1455+
14441456
/**
14451457
* Obtain the length of an array type.
14461458
*
14471459
* This only works on types that represent arrays.
14481460
*
1461+
* @deprecated LLVMGetArrayLength is deprecated in favor of the API accurate
1462+
* LLVMGetArrayLength2
14491463
* @see llvm::ArrayType::getNumElements()
14501464
*/
14511465
unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy);
14521466

1467+
/**
1468+
* Obtain the length of an array type.
1469+
*
1470+
* This only works on types that represent arrays.
1471+
*
1472+
* @see llvm::ArrayType::getNumElements()
1473+
*/
1474+
uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy);
1475+
14531476
/**
14541477
* Create a pointer type that points to a defined type.
14551478
*
@@ -2118,11 +2141,21 @@ LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
21182141
/**
21192142
* Create a ConstantArray from values.
21202143
*
2144+
* @deprecated LLVMConstArray is deprecated in favor of the API accurate
2145+
* LLVMConstArray2
21212146
* @see llvm::ConstantArray::get()
21222147
*/
21232148
LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
21242149
LLVMValueRef *ConstantVals, unsigned Length);
21252150

2151+
/**
2152+
* Create a ConstantArray from values.
2153+
*
2154+
* @see llvm::ConstantArray::get()
2155+
*/
2156+
LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
2157+
uint64_t Length);
2158+
21262159
/**
21272160
* Create a non-anonymous ConstantStruct from values.
21282161
*

llvm/lib/IR/Core.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,10 @@ LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) {
788788
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
789789
}
790790

791+
LLVMTypeRef LLVMArrayType2(LLVMTypeRef ElementType, uint64_t ElementCount) {
792+
return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
793+
}
794+
791795
LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) {
792796
return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
793797
}
@@ -822,6 +826,10 @@ unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
822826
return unwrap<ArrayType>(ArrayTy)->getNumElements();
823827
}
824828

829+
uint64_t LLVMGetArrayLength2(LLVMTypeRef ArrayTy) {
830+
return unwrap<ArrayType>(ArrayTy)->getNumElements();
831+
}
832+
825833
unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) {
826834
return unwrap<PointerType>(PointerTy)->getAddressSpace();
827835
}
@@ -1493,6 +1501,12 @@ LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
14931501
return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
14941502
}
14951503

1504+
LLVMValueRef LLVMConstArray2(LLVMTypeRef ElementTy, LLVMValueRef *ConstantVals,
1505+
uint64_t Length) {
1506+
ArrayRef<Constant *> V(unwrap<Constant>(ConstantVals, Length), Length);
1507+
return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1508+
}
1509+
14961510
LLVMValueRef LLVMConstStructInContext(LLVMContextRef C,
14971511
LLVMValueRef *ConstantVals,
14981512
unsigned Count, LLVMBool Packed) {

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ struct TypeCloner {
133133
return S;
134134
}
135135
case LLVMArrayTypeKind:
136-
return LLVMArrayType(
137-
Clone(LLVMGetElementType(Src)),
138-
LLVMGetArrayLength(Src)
139-
);
136+
return LLVMArrayType2(Clone(LLVMGetElementType(Src)),
137+
LLVMGetArrayLength2(Src));
140138
case LLVMPointerTypeKind:
141139
if (LLVMPointerTypeIsOpaque(Src))
142140
return LLVMPointerTypeInContext(Ctx, LLVMGetPointerAddressSpace(Src));
@@ -309,9 +307,9 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
309307
? LLVMConstantArrayValueKind
310308
: LLVMConstantDataArrayValueKind);
311309
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
312-
unsigned EltCount = LLVMGetArrayLength(Ty);
310+
uint64_t EltCount = LLVMGetArrayLength2(Ty);
313311
SmallVector<LLVMValueRef, 8> Elts;
314-
for (unsigned i = 0; i < EltCount; i++)
312+
for (uint64_t i = 0; i < EltCount; i++)
315313
Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
316314
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
317315
}

0 commit comments

Comments
 (0)