From 573a9bc4ad7fed92db5105ea75e0f9573712f973 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 4 Dec 2021 18:54:36 +0100 Subject: [PATCH] [llvm-c] Avoid deprecated APIs in tests Avoid the use of deprecated (opaque pointer incompatible) APIs in C API tests, in preparation for header deprecation. Add a LLVMGetGEPSourceElementType() to cover a bit of functionality that is necessary for the echo test. This change is split out from https://reviews.llvm.org/D114936. --- llvm/include/llvm-c/Core.h | 5 +++++ llvm/lib/IR/Core.cpp | 4 ++++ llvm/tools/llvm-c-test/calc.c | 5 +++-- llvm/tools/llvm-c-test/echo.cpp | 16 ++++++++++------ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index f2183ff..2efb4d0 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3507,6 +3507,11 @@ LLVMBool LLVMIsInBounds(LLVMValueRef GEP); void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds); /** + * Get the source element type of the given GEP instruction. + */ +LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP); + +/** * @} */ diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 2c396ae..6104a79 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3014,6 +3014,10 @@ void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { return unwrap(GEP)->setIsInBounds(InBounds); } +LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) { + return wrap(unwrap(GEP)->getSourceElementType()); +} + /*--.. Operations on phi nodes .............................................--*/ void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues, diff --git a/llvm/tools/llvm-c-test/calc.c b/llvm/tools/llvm-c-test/calc.c index bd7aff4..267397a 100644 --- a/llvm/tools/llvm-c-test/calc.c +++ b/llvm/tools/llvm-c-test/calc.c @@ -74,8 +74,9 @@ static LLVMValueRef build_from_tokens(char **tokens, int ntokens, return NULL; } - off = LLVMBuildGEP(builder, param, &stack[depth - 1], 1, ""); - stack[depth - 1] = LLVMBuildLoad(builder, off, ""); + LLVMTypeRef ty = LLVMInt64Type(); + off = LLVMBuildGEP2(builder, ty, param, &stack[depth - 1], 1, ""); + stack[depth - 1] = LLVMBuildLoad2(builder, ty, off, ""); break; } diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 4475cbe..61b7158 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -524,11 +524,12 @@ struct FunCloner { int ArgCount = LLVMGetNumArgOperands(Src); for (int i = 0; i < ArgCount; i++) Args.push_back(CloneValue(LLVMGetOperand(Src, i))); + LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); - Dst = LLVMBuildInvoke(Builder, Fn, Args.data(), ArgCount, - Then, Unwind, Name); + Dst = LLVMBuildInvoke2(Builder, FnTy, Fn, Args.data(), ArgCount, + Then, Unwind, Name); CloneAttrs(Src, Dst); break; } @@ -621,7 +622,7 @@ struct FunCloner { } case LLVMLoad: { LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); - Dst = LLVMBuildLoad(Builder, Ptr, Name); + Dst = LLVMBuildLoad2(Builder, CloneType(Src), Ptr, Name); LLVMSetAlignment(Dst, LLVMGetAlignment(Src)); LLVMSetOrdering(Dst, LLVMGetOrdering(Src)); LLVMSetVolatile(Dst, LLVMGetVolatile(Src)); @@ -637,15 +638,17 @@ struct FunCloner { break; } case LLVMGetElementPtr: { + LLVMTypeRef ElemTy = CloneType(LLVMGetGEPSourceElementType(Src)); LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0)); SmallVector Idx; int NumIdx = LLVMGetNumIndices(Src); for (int i = 1; i <= NumIdx; i++) Idx.push_back(CloneValue(LLVMGetOperand(Src, i))); if (LLVMIsInBounds(Src)) - Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name); + Dst = LLVMBuildInBoundsGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, + Name); else - Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name); + Dst = LLVMBuildGEP2(Builder, ElemTy, Ptr, Idx.data(), NumIdx, Name); break; } case LLVMAtomicRMW: { @@ -709,8 +712,9 @@ struct FunCloner { int ArgCount = LLVMGetNumArgOperands(Src); for (int i = 0; i < ArgCount; i++) Args.push_back(CloneValue(LLVMGetOperand(Src, i))); + LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); - Dst = LLVMBuildCall(Builder, Fn, Args.data(), ArgCount, Name); + Dst = LLVMBuildCall2(Builder, FnTy, Fn, Args.data(), ArgCount, Name); LLVMSetTailCall(Dst, LLVMIsTailCall(Src)); CloneAttrs(Src, Dst); break; -- 2.7.4