[llvm] f660af4 - [OpaquePtr] Support call instruction

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 23 11:18:41 PDT 2021


Author: Nikita Popov
Date: 2021-06-23T20:17:26+02:00
New Revision: f660af46e3df342245b6a0aacd8989c5a2212552

URL: https://github.com/llvm/llvm-project/commit/f660af46e3df342245b6a0aacd8989c5a2212552
DIFF: https://github.com/llvm/llvm-project/commit/f660af46e3df342245b6a0aacd8989c5a2212552.diff

LOG: [OpaquePtr] Support call instruction

Add support for call of opaque pointer, currently only possible for
indirect calls.

This requires a bit of special casing in LLParser, as calls do not
specify the callee operand type explicitly.

Differential Revision: https://reviews.llvm.org/D104740

Added: 
    

Modified: 
    llvm/lib/AsmParser/LLParser.cpp
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/lib/IR/Verifier.cpp
    llvm/test/Assembler/opaque-ptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index dccd1a7ee23a4..599ce6ff5d83c 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -1469,8 +1469,13 @@ static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy,
 }
 
 Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
-                                        Value *Val, bool /* IsCall */) {
-  if (Val->getType() == Ty)
+                                        Value *Val, bool IsCall) {
+  Type *ValTy = Val->getType();
+  if (ValTy == Ty)
+    return Val;
+  // For calls, we also allow opaque pointers.
+  if (IsCall && ValTy == PointerType::get(Ty->getContext(),
+                                          Ty->getPointerAddressSpace()))
     return Val;
   if (Ty->isLabelTy())
     error(Loc, "'" + Name + "' is not a basic block");

diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 1631dc344bea6..7d69bd42757d1 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -5266,7 +5266,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
             cast<PointerType>(Callee->getType())->getElementType());
         if (!FTy)
           return error("Callee is not of pointer to function type");
-      } else if (cast<PointerType>(Callee->getType())->getElementType() != FTy)
+      } else if (!OpTy->isOpaqueOrPointeeTypeMatches(FTy))
         return error("Explicit call type does not match pointee type of "
                      "callee operand");
       if (Record.size() < FTy->getNumParams() + OpNum)

diff  --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index 42d6abe8e69f8..75710d6c93c0c 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -467,8 +467,10 @@ ValueEnumerator::ValueEnumerator(const Module &M,
         if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
           EnumerateType(GEP->getSourceElementType());
         EnumerateType(I.getType());
-        if (const auto *Call = dyn_cast<CallBase>(&I))
+        if (const auto *Call = dyn_cast<CallBase>(&I)) {
           EnumerateAttributes(Call->getAttributes());
+          EnumerateType(Call->getFunctionType());
+        }
 
         // Enumerate metadata attached with this instruction.
         MDs.clear();

diff  --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 4a7ccebd50bfa..76341082a01e2 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -3124,10 +3124,7 @@ void Verifier::visitCallBase(CallBase &Call) {
          "Called function must be a pointer!", Call);
   PointerType *FPTy = cast<PointerType>(Call.getCalledOperand()->getType());
 
-  Assert(FPTy->getElementType()->isFunctionTy(),
-         "Called function is not pointer to function type!", Call);
-
-  Assert(FPTy->getElementType() == Call.getFunctionType(),
+  Assert(FPTy->isOpaqueOrPointeeTypeMatches(Call.getFunctionType()),
          "Called function is not the same type as the call!", Call);
 
   FunctionType *FTy = Call.getFunctionType();

diff  --git a/llvm/test/Assembler/opaque-ptr.ll b/llvm/test/Assembler/opaque-ptr.ll
index 2e964afa93e99..d4ce6a98af2a3 100644
--- a/llvm/test/Assembler/opaque-ptr.ll
+++ b/llvm/test/Assembler/opaque-ptr.ll
@@ -98,3 +98,19 @@ define void @atomicrmw(ptr %a, i32 %i) {
     %b = atomicrmw add ptr %a, i32 %i acquire
     ret void
 }
+
+; CHECK: define void @call(ptr %p)
+; CHECK:     call void %p()
+; CHECK:     ret void
+define void @call(ptr %p) {
+  call void %p()
+  ret void
+}
+
+; CHECK: define void @call_arg(ptr %p, i32 %a)
+; CHECK:     call void %p(i32 %a)
+; CHECK:     ret void
+define void @call_arg(ptr %p, i32 %a) {
+  call void %p(i32 %a)
+  ret void
+}


        


More information about the llvm-commits mailing list