@@ -8,6 +8,7 @@ SPDX-License-Identifier: MIT
88
99#include " AdaptorsCommon.h"
1010
11+ #include " llvm/ADT/SmallPtrSet.h"
1112#include " llvm/IR/Function.h"
1213#include " llvm/IR/Instructions.h"
1314
@@ -30,6 +31,45 @@ static void legalizeAttribute(Argument &Arg, Type *NewType,
3031
3132#endif
3233
34+ Type *getPtrElemType (Value *V) {
35+ #if VC_INTR_LLVM_VERSION_MAJOR < 14
36+ return VCINTR::Type::getNonOpaquePtrEltTy (V->getType ());
37+ #else // VC_INTR_LLVM_VERSION_MAJOR < 14
38+ #if VC_INTR_LLVM_VERSION_MAJOR < 17
39+ auto *PtrTy = cast<PointerType>(V->getType ());
40+ if (!PtrTy->isOpaque ())
41+ return VCINTR::Type::getNonOpaquePtrEltTy (PtrTy);
42+ #endif // VC_INTR_LLVM_VERSION_MAJOR < 17
43+ SmallPtrSet<Type *, 2 > ElemTys;
44+ SmallVector<Value *, 4 > Stack;
45+ Stack.push_back (V);
46+ while (!Stack.empty ()) {
47+ auto * Current = Stack.back ();
48+ Stack.pop_back ();
49+ for (auto *U : Current->users ()) {
50+ if (ElemTys.size () > 1 )
51+ return nullptr ;
52+ auto *I = dyn_cast<Instruction>(U);
53+ if (!I)
54+ continue ;
55+ if (auto *LI = dyn_cast<LoadInst>(I)) {
56+ if (Current == LI->getPointerOperand ())
57+ ElemTys.insert (LI->getType ());
58+ } else if (auto *SI = dyn_cast<StoreInst>(I)) {
59+ if (Current == SI->getPointerOperand ())
60+ ElemTys.insert (SI->getValueOperand ()->getType ());
61+ } else if (auto *GEPI = dyn_cast<GetElementPtrInst>(I)) {
62+ if (Current == GEPI->getPointerOperand ())
63+ ElemTys.insert (GEPI->getSourceElementType ());
64+ } else if (isa<BitCastInst>(I) || isa<AddrSpaceCastInst>(I)) {
65+ Stack.push_back (I);
66+ }
67+ }
68+ }
69+ return ElemTys.empty () ? nullptr : *ElemTys.begin ();
70+ #endif // VC_INTR_LLVM_VERSION_MAJOR < 14
71+ }
72+
3373void legalizeParamAttributes (Function *F) {
3474 assert (F && " Valid function ptr must be passed" );
3575
@@ -39,15 +79,12 @@ void legalizeParamAttributes(Function *F) {
3979 if (!PTy)
4080 continue ;
4181
82+ auto *ElemType = getPtrElemType (&Arg);
4283#if VC_INTR_LLVM_VERSION_MAJOR >= 13
43- #if VC_INTR_LLVM_VERSION_MAJOR < 17
44- if (PTy->isOpaque ())
45- #endif // VC_INTR_LLVM_VERSION_MAJOR < 18
84+ if (!ElemType)
4685 continue ;
4786#endif // VC_INTR_LLVM_VERSION_MAJOR >= 13
4887
49- auto *ElemType = VCINTR::Type::getNonOpaquePtrEltTy (PTy);
50-
5188 legalizeAttribute (Arg, ElemType, Attribute::ByVal);
5289
5390#if VC_INTR_LLVM_VERSION_MAJOR >= 11
0 commit comments