[clang] 2724c15 - [clang] Cache OpenCL types

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 7 09:23:16 PST 2022


Author: Arthur Eubanks
Date: 2022-02-07T09:23:04-08:00
New Revision: 2724c153f92909523e225d9231ad90818795f7a2

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

LOG: [clang] Cache OpenCL types

If we call CGOpenCLRuntime::convertOpenCLSpecificType() multiple times
we should get the same type back.

Reviewed By: svenvh

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

Added: 
    

Modified: 
    clang/lib/CodeGen/CGOpenCLRuntime.cpp
    clang/lib/CodeGen/CGOpenCLRuntime.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGOpenCLRuntime.cpp b/clang/lib/CodeGen/CGOpenCLRuntime.cpp
index dbe375294d179..2496782d95d8f 100644
--- a/clang/lib/CodeGen/CGOpenCLRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenCLRuntime.cpp
@@ -34,41 +34,46 @@ llvm::Type *CGOpenCLRuntime::convertOpenCLSpecificType(const Type *T) {
   assert(T->isOpenCLSpecificType() &&
          "Not an OpenCL specific type!");
 
-  llvm::LLVMContext& Ctx = CGM.getLLVMContext();
-  uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
-      CGM.getContext().getOpenCLTypeAddrSpace(T));
   switch (cast<BuiltinType>(T)->getKind()) {
   default:
     llvm_unreachable("Unexpected opencl builtin type!");
     return nullptr;
-#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
-  case BuiltinType::Id: \
-    return llvm::PointerType::get( \
-        llvm::StructType::create(Ctx, "opencl." #ImgType "_" #Suffix "_t"), \
-        AddrSpc);
+#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix)                   \
+  case BuiltinType::Id:                                                        \
+    return getPointerType(T, "opencl." #ImgType "_" #Suffix "_t");
 #include "clang/Basic/OpenCLImageTypes.def"
   case BuiltinType::OCLSampler:
     return getSamplerType(T);
   case BuiltinType::OCLEvent:
-    return llvm::PointerType::get(
-        llvm::StructType::create(Ctx, "opencl.event_t"), AddrSpc);
+    return getPointerType(T, "opencl.event_t");
   case BuiltinType::OCLClkEvent:
-    return llvm::PointerType::get(
-        llvm::StructType::create(Ctx, "opencl.clk_event_t"), AddrSpc);
+    return getPointerType(T, "opencl.clk_event_t");
   case BuiltinType::OCLQueue:
-    return llvm::PointerType::get(
-        llvm::StructType::create(Ctx, "opencl.queue_t"), AddrSpc);
+    return getPointerType(T, "opencl.queue_t");
   case BuiltinType::OCLReserveID:
-    return llvm::PointerType::get(
-        llvm::StructType::create(Ctx, "opencl.reserve_id_t"), AddrSpc);
-#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
-  case BuiltinType::Id: \
-    return llvm::PointerType::get( \
-        llvm::StructType::create(Ctx, "opencl." #ExtType), AddrSpc);
+    return getPointerType(T, "opencl.reserve_id_t");
+#define EXT_OPAQUE_TYPE(ExtType, Id, Ext)                                      \
+  case BuiltinType::Id:                                                        \
+    return getPointerType(T, "opencl." #ExtType);
 #include "clang/Basic/OpenCLExtensionTypes.def"
   }
 }
 
+llvm::PointerType *CGOpenCLRuntime::getPointerType(const Type *T,
+                                                   StringRef Name) {
+  auto I = CachedTys.find(Name);
+  if (I != CachedTys.end())
+    return I->second;
+
+  llvm::LLVMContext &Ctx = CGM.getLLVMContext();
+  uint32_t AddrSpc = CGM.getContext().getTargetAddressSpace(
+      CGM.getContext().getOpenCLTypeAddrSpace(T));
+  auto *PTy =
+      llvm::PointerType::get(llvm::StructType::create(Ctx, Name), AddrSpc);
+  CachedTys[Name] = PTy;
+  return PTy;
+}
+
 llvm::Type *CGOpenCLRuntime::getPipeType(const PipeType *T) {
   if (T->isReadOnly())
     return getPipeType(T, "opencl.pipe_ro_t", PipeROTy);

diff  --git a/clang/lib/CodeGen/CGOpenCLRuntime.h b/clang/lib/CodeGen/CGOpenCLRuntime.h
index 3f7aa9b0d8dc1..55515ab375786 100644
--- a/clang/lib/CodeGen/CGOpenCLRuntime.h
+++ b/clang/lib/CodeGen/CGOpenCLRuntime.h
@@ -18,6 +18,7 @@
 #include "clang/AST/Expr.h"
 #include "clang/AST/Type.h"
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/Value.h"
 
@@ -38,6 +39,7 @@ class CGOpenCLRuntime {
   llvm::Type *PipeROTy;
   llvm::Type *PipeWOTy;
   llvm::PointerType *SamplerTy;
+  llvm::StringMap<llvm::PointerType *> CachedTys;
 
   /// Structure for enqueued block information.
   struct EnqueuedBlockInfo {
@@ -50,6 +52,7 @@ class CGOpenCLRuntime {
 
   virtual llvm::Type *getPipeType(const PipeType *T, StringRef Name,
                                   llvm::Type *&PipeTy);
+  llvm::PointerType *getPointerType(const Type *T, StringRef Name);
 
 public:
   CGOpenCLRuntime(CodeGenModule &CGM) : CGM(CGM),


        


More information about the cfe-commits mailing list