[llvm] 5d4dd53 - Revert "[DirectX backend] Support global ctor for DXILBitcodeWriter."

Chris Bieneman via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 29 09:58:38 PDT 2022


Author: Chris Bieneman
Date: 2022-09-29T11:58:27-05:00
New Revision: 5d4dd5357076de54d70f8621a39626b393a6e110

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

LOG: Revert "[DirectX backend] Support global ctor for DXILBitcodeWriter."

This reverts commit 26129766df701d462ed9a6a9a68a88b3564a70bd.

The reverted commit broke in-tree unit tests for the DirectX backend.

Added: 
    

Modified: 
    llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
    llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp

Removed: 
    llvm/test/tools/dxil-dis/global_ctor.ll


################################################################################
diff  --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index 09909a252ee9f..4b9447340b34a 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -346,11 +346,7 @@ class DXILBitcodeWriter {
   unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); }
 
   unsigned getTypeID(Type *T, const Value *V = nullptr);
-  /// getGlobalObjectValueTypeID - returns the element type for a GlobalObject
-  ///
-  /// GlobalObject types are saved by PointerTypeAnalysis as pointers to the
-  /// GlobalObject, but in the bitcode writer we need the pointer element type.
-  unsigned getGlobalObjectValueTypeID(Type *T, const GlobalObject *G);
+  unsigned getTypeID(Type *T, const Function *F);
 };
 
 } // namespace dxil
@@ -555,30 +551,18 @@ unsigned DXILBitcodeWriter::getEncodedBinaryOpcode(unsigned Opcode) {
 }
 
 unsigned DXILBitcodeWriter::getTypeID(Type *T, const Value *V) {
-  if (!T->isOpaquePointerTy() &&
-      // For Constant, always check PointerMap to make sure OpaquePointer in
-      // things like constant struct/array works.
-      (!V || !isa<Constant>(V)))
+  if (!T->isOpaquePointerTy())
     return VE.getTypeID(T);
   auto It = PointerMap.find(V);
   if (It != PointerMap.end())
     return VE.getTypeID(It->second);
-  // For Constant, return T when cannot find in PointerMap.
-  // FIXME: support ConstantPointerNull which could map to more than one
-  // TypedPointerType.
-  // See https://github.com/llvm/llvm-project/issues/57942.
-  if (V && isa<Constant>(V) && !isa<ConstantPointerNull>(V))
-    return VE.getTypeID(T);
   return VE.getTypeID(I8PtrTy);
 }
 
-unsigned DXILBitcodeWriter::getGlobalObjectValueTypeID(Type *T,
-                                                       const GlobalObject *G) {
-  auto It = PointerMap.find(G);
-  if (It != PointerMap.end()) {
-    TypedPointerType *PtrTy = cast<TypedPointerType>(It->second);
-    return VE.getTypeID(PtrTy->getElementType());
-  }
+unsigned DXILBitcodeWriter::getTypeID(Type *T, const Function *F) {
+  auto It = PointerMap.find(F);
+  if (It != PointerMap.end())
+    return VE.getTypeID(It->second);
   return VE.getTypeID(T);
 }
 
@@ -1225,10 +1209,7 @@ void DXILBitcodeWriter::writeModuleInfo() {
   };
   for (const GlobalVariable &GV : M.globals()) {
     UpdateMaxAlignment(GV.getAlign());
-    // Use getGlobalObjectValueTypeID to look up the enumerated type ID for
-    // Global Variable types.
-    MaxGlobalType = std::max(
-        MaxGlobalType, getGlobalObjectValueTypeID(GV.getValueType(), &GV));
+    MaxGlobalType = std::max(MaxGlobalType, getTypeID(GV.getValueType(), &GV));
     if (GV.hasSection()) {
       // Give section names unique ID's.
       unsigned &Entry = SectionMap[std::string(GV.getSection())];
@@ -1300,7 +1281,7 @@ void DXILBitcodeWriter::writeModuleInfo() {
     //             linkage, alignment, section, visibility, threadlocal,
     //             unnamed_addr, externally_initialized, dllstorageclass,
     //             comdat]
-    Vals.push_back(getGlobalObjectValueTypeID(GV.getValueType(), &GV));
+    Vals.push_back(getTypeID(GV.getValueType(), &GV));
     Vals.push_back(
         GV.getType()->getAddressSpace() << 2 | 2 |
         (GV.isConstant() ? 1 : 0)); // HLSL Change - bitwise | was used with
@@ -1336,7 +1317,7 @@ void DXILBitcodeWriter::writeModuleInfo() {
     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
     //             section, visibility, gc, unnamed_addr, prologuedata,
     //             dllstorageclass, comdat, prefixdata, personalityfn]
-    Vals.push_back(getGlobalObjectValueTypeID(F.getFunctionType(), &F));
+    Vals.push_back(getTypeID(F.getFunctionType(), &F));
     Vals.push_back(F.getCallingConv());
     Vals.push_back(F.isDeclaration());
     Vals.push_back(getEncodedLinkage(F));
@@ -1990,7 +1971,7 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
     // If we need to switch types, do so now.
     if (V->getType() != LastTy) {
       LastTy = V->getType();
-      Record.push_back(getTypeID(LastTy, V));
+      Record.push_back(getTypeID(LastTy));
       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
                         CONSTANTS_SETTYPE_ABBREV);
       Record.clear();
@@ -2125,8 +2106,7 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
         if (Instruction::isCast(CE->getOpcode())) {
           Code = bitc::CST_CODE_CE_CAST;
           Record.push_back(getEncodedCastOpcode(CE->getOpcode()));
-          Record.push_back(
-              getTypeID(C->getOperand(0)->getType(), C->getOperand(0)));
+          Record.push_back(getTypeID(C->getOperand(0)->getType()));
           Record.push_back(VE.getValueID(C->getOperand(0)));
           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
         } else {
@@ -2147,8 +2127,7 @@ void DXILBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
         Record.push_back(getTypeID(GO->getSourceElementType()));
         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
-          Record.push_back(
-              getTypeID(C->getOperand(i)->getType(), C->getOperand(i)));
+          Record.push_back(getTypeID(C->getOperand(i)->getType()));
           Record.push_back(VE.getValueID(C->getOperand(i)));
         }
         break;
@@ -2550,7 +2529,7 @@ void DXILBitcodeWriter::writeInstruction(const Instruction &I, unsigned InstID,
     Vals.push_back(VE.getAttributeListID(CI.getAttributes()));
     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
                    unsigned(CI.isMustTailCall()) << 14 | 1 << 15);
-    Vals.push_back(getGlobalObjectValueTypeID(FTy, CI.getCalledFunction()));
+    Vals.push_back(getTypeID(FTy, CI.getCalledFunction()));
     pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee
 
     // Emit value #'s for the fixed parameters.

diff  --git a/llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp b/llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
index b6de68681e9a8..1d536bbd00114 100644
--- a/llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/PointerTypeAnalysis.cpp
@@ -11,7 +11,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "PointerTypeAnalysis.h"
-#include "llvm/IR/Constants.h"
 #include "llvm/IR/Instructions.h"
 
 using namespace llvm;
@@ -21,32 +20,22 @@ namespace {
 
 // Classifies the type of the value passed in by walking the value's users to
 // find a typed instruction to materialize a type from.
-Type *classifyPointerType(const Value *V, PointerTypeMap &Map) {
+TypedPointerType *classifyPointerType(const Value *V) {
   assert(V->getType()->isOpaquePointerTy() &&
          "classifyPointerType called with non-opaque pointer");
-  auto It = Map.find(V);
-  if (It != Map.end())
-    return It->second;
-
   Type *PointeeTy = nullptr;
   if (auto *Inst = dyn_cast<GetElementPtrInst>(V)) {
     if (!Inst->getResultElementType()->isOpaquePointerTy())
       PointeeTy = Inst->getResultElementType();
   } else if (auto *Inst = dyn_cast<AllocaInst>(V)) {
     PointeeTy = Inst->getAllocatedType();
-  } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
-    PointeeTy = GV->getValueType();
   }
-
   for (const auto *User : V->users()) {
     Type *NewPointeeTy = nullptr;
     if (const auto *Inst = dyn_cast<LoadInst>(User)) {
       NewPointeeTy = Inst->getType();
     } else if (const auto *Inst = dyn_cast<StoreInst>(User)) {
       NewPointeeTy = Inst->getValueOperand()->getType();
-      // When store value is ptr type, cannot get more type info.
-      if (NewPointeeTy->isOpaquePointerTy())
-        continue;
     } else if (const auto *Inst = dyn_cast<GetElementPtrInst>(User)) {
       NewPointeeTy = Inst->getSourceElementType();
     }
@@ -55,7 +44,7 @@ Type *classifyPointerType(const Value *V, PointerTypeMap &Map) {
       // or two levels of indirection in the IR. Because of this, recursion is
       // pretty safe.
       if (NewPointeeTy->isOpaquePointerTy())
-        return TypedPointerType::get(classifyPointerType(User, Map),
+        return TypedPointerType::get(classifyPointerType(User),
                                      V->getType()->getPointerAddressSpace());
       if (!PointeeTy)
         PointeeTy = NewPointeeTy;
@@ -66,143 +55,65 @@ Type *classifyPointerType(const Value *V, PointerTypeMap &Map) {
   // If we were unable to determine the pointee type, set to i8
   if (!PointeeTy)
     PointeeTy = Type::getInt8Ty(V->getContext());
-  auto *TypedPtrTy =
-      TypedPointerType::get(PointeeTy, V->getType()->getPointerAddressSpace());
-
-  Map[V] = TypedPtrTy;
-  return TypedPtrTy;
+  return TypedPointerType::get(PointeeTy,
+                               V->getType()->getPointerAddressSpace());
 }
 
 // This function constructs a function type accepting typed pointers. It only
 // handles function arguments and return types, and assigns the function type to
 // the function's value in the type map.
-Type *classifyFunctionType(const Function &F, PointerTypeMap &Map) {
-  auto It = Map.find(&F);
-  if (It != Map.end())
-    return It->second;
-
+void classifyFunctionType(const Function &F, PointerTypeMap &Map) {
   SmallVector<Type *, 8> NewArgs;
+  bool HasOpaqueTy = false;
   Type *RetTy = F.getReturnType();
-  LLVMContext &Ctx = F.getContext();
   if (RetTy->isOpaquePointerTy()) {
     RetTy = nullptr;
     for (const auto &B : F) {
-      const auto *RetInst = dyn_cast_or_null<ReturnInst>(B.getTerminator());
-      if (!RetInst)
-        continue;
-
-      Type *NewRetTy = classifyPointerType(RetInst->getReturnValue(), Map);
-      if (!RetTy)
-        RetTy = NewRetTy;
-      else if (RetTy != NewRetTy)
-        RetTy = TypedPointerType::get(
-            Type::getInt8Ty(Ctx), F.getReturnType()->getPointerAddressSpace());
+      for (const auto &I : B) {
+        if (const auto *RetInst = dyn_cast_or_null<ReturnInst>(&I)) {
+          Type *NewRetTy = classifyPointerType(RetInst->getReturnValue());
+          if (!RetTy)
+            RetTy = NewRetTy;
+          else if (RetTy != NewRetTy)
+            RetTy = TypedPointerType::get(
+                Type::getInt8Ty(I.getContext()),
+                F.getReturnType()->getPointerAddressSpace());
+        }
+      }
     }
-    // For function decl.
-    if (!RetTy)
-      RetTy = TypedPointerType::get(
-          Type::getInt8Ty(Ctx), F.getReturnType()->getPointerAddressSpace());
   }
   for (auto &A : F.args()) {
     Type *ArgTy = A.getType();
-    if (ArgTy->isOpaquePointerTy())
-      ArgTy = classifyPointerType(&A, Map);
+    if (ArgTy->isOpaquePointerTy()) {
+      TypedPointerType *NewTy = classifyPointerType(&A);
+      Map[&A] = NewTy;
+      ArgTy = NewTy;
+      HasOpaqueTy = true;
+    }
     NewArgs.push_back(ArgTy);
   }
-  auto *TypedPtrTy =
-      TypedPointerType::get(FunctionType::get(RetTy, NewArgs, false), 0);
-  Map[&F] = TypedPtrTy;
-  return TypedPtrTy;
+  if (!HasOpaqueTy)
+    return;
+  Map[&F] = FunctionType::get(RetTy, NewArgs, false);
 }
 } // anonymous namespace
 
-static Type *classifyConstantWithOpaquePtr(const Constant *C,
-                                           PointerTypeMap &Map) {
-  // FIXME: support ConstantPointerNull which could map to more than one
-  // TypedPointerType.
-  // See https://github.com/llvm/llvm-project/issues/57942.
-  if (isa<ConstantPointerNull>(C))
-    return TypedPointerType::get(Type::getInt8Ty(C->getContext()),
-                                 C->getType()->getPointerAddressSpace());
-
-  // Skip ConstantData which cannot have opaque ptr.
-  if (isa<ConstantData>(C))
-    return C->getType();
-
-  auto It = Map.find(C);
-  if (It != Map.end())
-    return It->second;
-
-  if (const auto *F = dyn_cast<Function>(C))
-    return classifyFunctionType(*F, Map);
-
-  Type *Ty = C->getType();
-  Type *TargetTy = nullptr;
-  if (auto *CS = dyn_cast<ConstantStruct>(C)) {
-    SmallVector<Type *> EltTys;
-    for (unsigned int I = 0; I < CS->getNumOperands(); ++I) {
-      const Constant *Elt = C->getAggregateElement(I);
-      Type *EltTy = classifyConstantWithOpaquePtr(Elt, Map);
-      EltTys.emplace_back(EltTy);
-    }
-    TargetTy = StructType::get(C->getContext(), EltTys);
-  } else if (auto *CA = dyn_cast<ConstantAggregate>(C)) {
-
-    Type *TargetEltTy = nullptr;
-    for (auto &Elt : CA->operands()) {
-      Type *EltTy = classifyConstantWithOpaquePtr(cast<Constant>(&Elt), Map);
-      assert(TargetEltTy == EltTy || TargetEltTy == nullptr);
-      TargetEltTy = EltTy;
-    }
-
-    if (auto *AT = dyn_cast<ArrayType>(Ty)) {
-      TargetTy = ArrayType::get(TargetEltTy, AT->getNumElements());
-    } else {
-      // Not struct, not array, must be vector here.
-      auto *VT = cast<VectorType>(Ty);
-      TargetTy = VectorType::get(TargetEltTy, VT);
-    }
-  }
-  // Must have a target ty when map.
-  assert(TargetTy && "PointerTypeAnalyisis failed to identify target type");
-
-  // Same type, no need to map.
-  if (TargetTy == Ty)
-    return Ty;
-
-  Map[C] = TargetTy;
-  return TargetTy;
-}
-
-static void classifyGlobalCtorPointerType(const GlobalVariable &GV,
-                                          PointerTypeMap &Map) {
-  const auto *CA = cast<ConstantArray>(GV.getInitializer());
-  // Type for global ctor should be array of { i32, void ()*, i8* }.
-  Type *CtorArrayTy = classifyConstantWithOpaquePtr(CA, Map);
-
-  // Map the global type.
-  Map[&GV] = TypedPointerType::get(CtorArrayTy,
-                                   GV.getType()->getPointerAddressSpace());
-}
-
 PointerTypeMap PointerTypeAnalysis::run(const Module &M) {
   PointerTypeMap Map;
   for (auto &G : M.globals()) {
     if (G.getType()->isOpaquePointerTy())
-      classifyPointerType(&G, Map);
-    if (G.getName() == "llvm.global_ctors")
-      classifyGlobalCtorPointerType(G, Map);
+      Map[&G] = classifyPointerType(&G);
   }
-
   for (auto &F : M) {
     classifyFunctionType(F, Map);
 
     for (const auto &B : F) {
       for (const auto &I : B) {
         if (I.getType()->isOpaquePointerTy())
-          classifyPointerType(&I, Map);
+          Map[&I] = classifyPointerType(&I);
       }
     }
   }
+
   return Map;
 }

diff  --git a/llvm/test/tools/dxil-dis/global_ctor.ll b/llvm/test/tools/dxil-dis/global_ctor.ll
deleted file mode 100644
index 55c4f7e8709dc..0000000000000
--- a/llvm/test/tools/dxil-dis/global_ctor.ll
+++ /dev/null
@@ -1,54 +0,0 @@
-; RUN: llc --filetype=obj %s -o - 2>&1 | dxil-dis -o - | FileCheck %s
-
-target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
-target triple = "dxil-unknown-shadermodel6.7-library"
-; Make sure global ctor type is changed to void ()*.
-; CHECK:@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @_GLOBAL__sub_I_static_global.hlsl, i8* null }]
-
- at f = internal unnamed_addr global float 0.000000e+00, align 4
- at llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_global.hlsl, ptr null }]
-
-declare float @"?init@@YAMXZ"() local_unnamed_addr #0
-
-; Function Attrs: nounwind
-define float @"?foo@@YAMXZ"() local_unnamed_addr #1 {
-entry:
-  %0 = load float, ptr @f, align 4, !tbaa !4
-  %inc = fadd float %0, 1.000000e+00
-  store float %inc, ptr @f, align 4, !tbaa !4
-  ret float %0
-}
-
-; Function Attrs: nounwind
-define float @"?bar@@YAMXZ"() local_unnamed_addr #1 {
-entry:
-  %0 = load float, ptr @f, align 4, !tbaa !4
-  %dec = fadd float %0, -1.000000e+00
-  store float %dec, ptr @f, align 4, !tbaa !4
-  ret float %0
-}
-
-; Function Attrs: nounwind
-define internal void @_GLOBAL__sub_I_static_global.hlsl() #1 {
-entry:
-  %call.i = tail call float @"?init@@YAMXZ"() #2
-  store float %call.i, ptr @f, align 4, !tbaa !4
-  ret void
-}
-
-attributes #0 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #1 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
-attributes #2 = { nounwind }
-
-!llvm.module.flags = !{!0, !1}
-!llvm.ident = !{!2}
-!dx.valver = !{!3}
-
-!0 = !{i32 1, !"wchar_size", i32 4}
-!1 = !{i32 7, !"frame-pointer", i32 2}
-!2 = !{!"clang version 16.0.0 (https://github.com/llvm/llvm-project.git c5dfff0e58cc66d74e666c31368f6d44328dd2f7)"}
-!3 = !{i32 1, i32 7}
-!4 = !{!5, !5, i64 0}
-!5 = !{!"float", !6, i64 0}
-!6 = !{!"omnipotent char", !7, i64 0}
-!7 = !{!"Simple C++ TBAA"}


        


More information about the llvm-commits mailing list