[llvm] 46cfbe5 - [LLVMContext] Replace enableOpaquePointers() with setOpaquePointers()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 5 03:02:55 PDT 2022


Author: Nikita Popov
Date: 2022-04-05T12:02:48+02:00
New Revision: 46cfbe561bfd3b35984b3e1662bd2bca4eb9e96d

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

LOG: [LLVMContext] Replace enableOpaquePointers() with setOpaquePointers()

This allows both explicitly enabling and explicitly disabling
opaque pointers, in anticipation of the default switching at some
point.

This also slightly changes the rules by allowing calls if either
the opaque pointer mode has not yet been set (explicitly or
implicitly) or if the value remains unchanged.

Added: 
    

Modified: 
    clang/lib/CodeGen/CodeGenAction.cpp
    llvm/include/llvm/IR/LLVMContext.h
    llvm/lib/AsmParser/LLLexer.cpp
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/lib/IR/LLVMContext.cpp
    llvm/lib/IR/LLVMContextImpl.cpp
    llvm/unittests/IR/ConstantsTest.cpp
    llvm/unittests/IR/TypesTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index 25f4cbbc700a5..b8ed3534de8be 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -984,7 +984,7 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
     return nullptr;
 
   if (CI.getCodeGenOpts().OpaquePointers)
-    VMContext->enableOpaquePointers();
+    VMContext->setOpaquePointers(true);
 
   // Load bitcode modules to link with, if we need to.
   if (LinkModules.empty())

diff  --git a/llvm/include/llvm/IR/LLVMContext.h b/llvm/include/llvm/IR/LLVMContext.h
index d80d54693258a..73e2326b8fe3e 100644
--- a/llvm/include/llvm/IR/LLVMContext.h
+++ b/llvm/include/llvm/IR/LLVMContext.h
@@ -308,9 +308,11 @@ class LLVMContext {
   /// Whether we've decided on using opaque pointers or typed pointers yet.
   bool hasSetOpaquePointersValue() const;
 
-  /// Enable opaque pointers. Can only be called before creating the first
-  /// pointer type.
-  void enableOpaquePointers() const;
+  /// Set whether opaque pointers are enabled. The method may be called multiple
+  /// times, but only with the same value. Note that creating a pointer type or
+  /// otherwise querying the opaque pointer mode performs an implicit set to
+  /// the default value.
+  void setOpaquePointers(bool Enable) const;
 
   /// Whether typed pointers are supported. If false, all pointers are opaque.
   bool supportsTypedPointers() const;

diff  --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 8aac5a66c6232..2a091a9c94f8e 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -860,9 +860,9 @@ lltok::Kind LLLexer::LexIdentifier() {
   TYPEKEYWORD("token",     Type::getTokenTy(Context));
 
   if (Keyword == "ptr") {
-    // enableOpaquePointers() must be called before creating any pointer types.
+    // setOpaquePointers() must be called before creating any pointer types.
     if (!Context.hasSetOpaquePointersValue()) {
-      Context.enableOpaquePointers();
+      Context.setOpaquePointers(true);
     } else if (Context.supportsTypedPointers()) {
       Warning("ptr type is only supported in -opaque-pointers mode");
       return lltok::Error;

diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 034fa5832ba84..8a5074941b979 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1884,7 +1884,7 @@ Error BitcodeReader::parseTypeTableBody() {
       if (Record.size() != 1)
         return error("Invalid opaque pointer record");
       if (LLVM_UNLIKELY(!Context.hasSetOpaquePointersValue())) {
-        Context.enableOpaquePointers();
+        Context.setOpaquePointers(true);
       } else if (Context.supportsTypedPointers())
         return error(
             "Opaque pointers are only supported in -opaque-pointers mode");

diff  --git a/llvm/lib/IR/LLVMContext.cpp b/llvm/lib/IR/LLVMContext.cpp
index f4e917cc89920..c9b0d957aa887 100644
--- a/llvm/lib/IR/LLVMContext.cpp
+++ b/llvm/lib/IR/LLVMContext.cpp
@@ -355,10 +355,8 @@ bool LLVMContext::hasSetOpaquePointersValue() const {
   return pImpl->hasOpaquePointersValue();
 }
 
-void LLVMContext::enableOpaquePointers() const {
-  assert(pImpl->PointerTypes.empty() && pImpl->ASPointerTypes.empty() &&
-         "Must be called before creating any pointer types");
-  pImpl->setOpaquePointers(true);
+void LLVMContext::setOpaquePointers(bool Enable) const {
+  pImpl->setOpaquePointers(Enable);
 }
 
 bool LLVMContext::supportsTypedPointers() const {

diff  --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp
index 1d115e50021b2..ed192275aabed 100644
--- a/llvm/lib/IR/LLVMContextImpl.cpp
+++ b/llvm/lib/IR/LLVMContextImpl.cpp
@@ -259,4 +259,8 @@ bool LLVMContextImpl::getOpaquePointers() {
   return *OpaquePointers;
 }
 
-void LLVMContextImpl::setOpaquePointers(bool OP) { OpaquePointers = OP; }
+void LLVMContextImpl::setOpaquePointers(bool OP) {
+  assert((!OpaquePointers.hasValue() || OpaquePointers.getValue() == OP) &&
+         "Cannot change opaque pointers mode once set");
+  OpaquePointers = OP;
+}

diff  --git a/llvm/unittests/IR/ConstantsTest.cpp b/llvm/unittests/IR/ConstantsTest.cpp
index 98975dcf18c89..ad43fbcb0531f 100644
--- a/llvm/unittests/IR/ConstantsTest.cpp
+++ b/llvm/unittests/IR/ConstantsTest.cpp
@@ -471,9 +471,8 @@ TEST(ConstantsTest, BuildConstantDataVectors) {
 
 void bitcastToGEPHelper(bool useOpaquePointers) {
   LLVMContext Context;
+  Context.setOpaquePointers(useOpaquePointers);
   std::unique_ptr<Module> M(new Module("MyModule", Context));
-  if (useOpaquePointers)
-    Context.enableOpaquePointers();
 
   auto *i32 = Type::getInt32Ty(Context);
   auto *U = StructType::create(Context, "Unsized");

diff  --git a/llvm/unittests/IR/TypesTest.cpp b/llvm/unittests/IR/TypesTest.cpp
index 412bd0707c814..d8b605fb6a581 100644
--- a/llvm/unittests/IR/TypesTest.cpp
+++ b/llvm/unittests/IR/TypesTest.cpp
@@ -36,7 +36,7 @@ TEST(TypesTest, LayoutIdenticalEmptyStructs) {
 
 TEST(TypesTest, CopyPointerType) {
   LLVMContext COpaquePointers;
-  COpaquePointers.enableOpaquePointers();
+  COpaquePointers.setOpaquePointers(true);
 
   PointerType *P1 = PointerType::get(COpaquePointers, 1);
   EXPECT_TRUE(P1->isOpaque());


        


More information about the llvm-commits mailing list