[PATCH] D105155: [OpaquePtr] Support opaque pointers in intrinsic type check

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 29 14:59:22 PDT 2021


nikic created this revision.
nikic added a reviewer: opaque-pointers.
Herald added subscribers: ormris, dexonsmith, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This adds partial support for opaque pointers in intrinsic type checks. Specifically, this handles the case where a fixed pointer type is specified.

This is less straight-forward than it might initially seem, because we should only accept opaque pointers here in `--force-opaque-pointers` mode. Otherwise, there would be more than one valid type signature for a given intrinsic name.

I'm accessing the flag on LLVMContextImpl, but possible the ForceOpaquePointers mode should be part of the public LLVM context API?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105155

Files:
  llvm/lib/IR/Function.cpp
  llvm/test/Assembler/remangle-intrinsic-opaque-ptr.ll
  llvm/test/Other/force-opaque-ptrs.ll


Index: llvm/test/Other/force-opaque-ptrs.ll
===================================================================
--- llvm/test/Other/force-opaque-ptrs.ll
+++ llvm/test/Other/force-opaque-ptrs.ll
@@ -48,3 +48,19 @@
 ;
   unreachable
 }
+
+define void @remangle_intrinsic() {
+; CHECK-LABEL: define {{[^@]+}}@remangle_intrinsic() {
+; CHECK-NEXT:    [[A:%.*]] = alloca ptr, align 8
+; CHECK-NEXT:    [[TMP1:%.*]] = call ptr @llvm.stacksave()
+; CHECK-NEXT:    call void @llvm.stackprotector(ptr null, ptr [[A]])
+; CHECK-NEXT:    ret void
+;
+  %a = alloca i8*
+  call i8* @llvm.stacksave()
+  call void @llvm.stackprotector(i8* null, i8** %a)
+  ret void
+}
+
+declare i8* @llvm.stacksave()
+declare void @llvm.stackprotector(i8*, i8**)
Index: llvm/test/Assembler/remangle-intrinsic-opaque-ptr.ll
===================================================================
--- /dev/null
+++ llvm/test/Assembler/remangle-intrinsic-opaque-ptr.ll
@@ -0,0 +1,12 @@
+; RUN: not llvm-as < %s -disable-output 2>&1 | FileCheck %s
+
+; An opaque pointer type should not be accepted for an intrinsic that
+; specifies a fixed pointer type, outside of --force-opaque-pointers mode.
+
+; CHECK: Intrinsic has incorrect return type!
+define void @test() {
+  call ptr @llvm.stacksave()
+  ret void
+}
+
+declare ptr @llvm.stacksave()
Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/IR/Function.h"
+#include "LLVMContextImpl.h"
 #include "SymbolTableListTraitsImpl.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseSet.h"
@@ -1404,9 +1405,22 @@
     }
     case IITDescriptor::Pointer: {
       PointerType *PT = dyn_cast<PointerType>(Ty);
-      return !PT || PT->getAddressSpace() != D.Pointer_AddressSpace ||
-             matchIntrinsicType(PT->getElementType(), Infos, ArgTys,
-                                DeferredChecks, IsDeferredCheck);
+      if (!PT || PT->getAddressSpace() != D.Pointer_AddressSpace)
+        return true;
+      if (!PT->isOpaque())
+        return matchIntrinsicType(PT->getElementType(), Infos, ArgTys,
+                                  DeferredChecks, IsDeferredCheck);
+      // When not using --force-opaque-pointers, do not allow using opaque
+      // pointer in place of fixed pointer type. This would make the intrinsic
+      // signature non-unique.
+      LLVMContextImpl *CImpl = Ty->getContext().pImpl;
+      if (!CImpl->ForceOpaquePointers)
+        return true;
+      // Consume IIT descriptors relating to the pointer element type.
+      while (Infos.front().Kind == IITDescriptor::Pointer)
+        Infos = Infos.slice(1);
+      Infos = Infos.slice(1);
+      return false;
     }
 
     case IITDescriptor::Struct: {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105155.355372.patch
Type: text/x-patch
Size: 2888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210629/fca98234/attachment.bin>


More information about the llvm-commits mailing list