@@ -107,6 +107,10 @@ static cl::opt<bool> PreserveAssemblyUseListOrder(
107107 " preserve-ll-uselistorder" , cl::Hidden, cl::init(false ),
108108 cl::desc(" Preserve use-list order when writing LLVM assembly." ));
109109
110+ static cl::opt<bool > PrintAddrspaceName (" print-addrspace-name" , cl::Hidden,
111+ cl::init (false ),
112+ cl::desc(" Print address space names" ));
113+
110114// Make virtual table appear in this compilation unit.
111115AssemblyAnnotationWriter::~AssemblyAnnotationWriter () = default ;
112116
@@ -543,7 +547,8 @@ namespace {
543547
544548class TypePrinting {
545549public:
546- TypePrinting (const Module *M = nullptr ) : DeferredM(M) {}
550+ TypePrinting (const Module *M = nullptr )
551+ : M(M), TypesIncorporated(M == nullptr ) {}
547552
548553 TypePrinting (const TypePrinting &) = delete ;
549554 TypePrinting &operator =(const TypePrinting &) = delete ;
@@ -563,8 +568,9 @@ class TypePrinting {
563568private:
564569 void incorporateTypes ();
565570
566- // / A module to process lazily when needed. Set to nullptr as soon as used.
567- const Module *DeferredM;
571+ // / A module to process lazily.
572+ const Module *M;
573+ bool TypesIncorporated;
568574
569575 TypeFinder NamedTypes;
570576
@@ -605,11 +611,11 @@ bool TypePrinting::empty() {
605611}
606612
607613void TypePrinting::incorporateTypes () {
608- if (!DeferredM )
614+ if (TypesIncorporated )
609615 return ;
610616
611- NamedTypes.run (*DeferredM , false );
612- DeferredM = nullptr ;
617+ NamedTypes.run (*M , false );
618+ TypesIncorporated = true ;
613619
614620 // The list of struct types we got back includes all the struct types, split
615621 // the unnamed ones out to a numbering and remove the anonymous structs.
@@ -630,6 +636,21 @@ void TypePrinting::incorporateTypes() {
630636 NamedTypes.erase (NextToUse, NamedTypes.end ());
631637}
632638
639+ static void printAddressSpace (const Module *M, unsigned AS, raw_ostream &OS,
640+ StringRef Prefix = " " , StringRef Suffix = " " ,
641+ bool ForcePrint = false ) {
642+ if (AS == 0 && !ForcePrint)
643+ return ;
644+ OS << Prefix << " addrspace(" ;
645+ StringRef ASName =
646+ PrintAddrspaceName && M ? M->getDataLayout ().getAddressSpaceName (AS) : " " ;
647+ if (!ASName.empty ())
648+ OS << " \" " << ASName << " \" " ;
649+ else
650+ OS << AS;
651+ OS << " )" << Suffix;
652+ }
653+
633654// / Write the specified type to the specified raw_ostream, making use of type
634655// / names or up references to shorten the type name where possible.
635656void TypePrinting::print (Type *Ty, raw_ostream &OS) {
@@ -686,8 +707,7 @@ void TypePrinting::print(Type *Ty, raw_ostream &OS) {
686707 case Type::PointerTyID: {
687708 PointerType *PTy = cast<PointerType>(Ty);
688709 OS << " ptr" ;
689- if (unsigned AddressSpace = PTy->getAddressSpace ())
690- OS << " addrspace(" << AddressSpace << ' )' ;
710+ printAddressSpace (M, PTy->getAddressSpace (), OS);
691711 return ;
692712 }
693713 case Type::ArrayTyID: {
@@ -3896,10 +3916,10 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
38963916 printThreadLocalModel (GV->getThreadLocalMode (), Out);
38973917 StringRef UA = getUnnamedAddrEncoding (GV->getUnnamedAddr ());
38983918 if (!UA.empty ())
3899- Out << UA << ' ' ;
3919+ Out << UA << ' ' ;
39003920
3901- if ( unsigned AddressSpace = GV->getType ()->getAddressSpace ())
3902- Out << " addrspace( " << AddressSpace << " ) " ;
3921+ printAddressSpace (GV-> getParent (), GV->getType ()->getAddressSpace (), Out,
3922+ /* Prefix= */ " " , /* Suffix= */ " " ) ;
39033923 if (GV->isExternallyInitialized ()) Out << " externally_initialized " ;
39043924 Out << (GV->isConstant () ? " constant " : " global " );
39053925 TypePrinter.print (GV->getValueType (), Out);
@@ -4174,9 +4194,10 @@ void AssemblyWriter::printFunction(const Function *F) {
41744194 // a module with a non-zero program address space or if there is no valid
41754195 // Module* so that the file can be parsed without the datalayout string.
41764196 const Module *Mod = F->getParent ();
4177- if (F->getAddressSpace () != 0 || !Mod ||
4178- Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4179- Out << " addrspace(" << F->getAddressSpace () << " )" ;
4197+ bool ForcePrintAddressSpace =
4198+ !Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 ;
4199+ printAddressSpace (Mod, F->getAddressSpace (), Out, /* Prefix=*/ " " ,
4200+ /* Suffix=*/ " " , ForcePrintAddressSpace);
41804201 if (Attrs.hasFnAttrs ())
41814202 Out << " #" << Machine.getAttributeGroupSlot (Attrs.getFnAttrs ());
41824203 if (F->hasSection ()) {
@@ -4352,23 +4373,21 @@ void AssemblyWriter::printInfoComment(const Value &V, bool isMaterializable) {
43524373
43534374static void maybePrintCallAddrSpace (const Value *Operand, const Instruction *I,
43544375 raw_ostream &Out) {
4355- // We print the address space of the call if it is non-zero.
43564376 if (Operand == nullptr ) {
43574377 Out << " <cannot get addrspace!>" ;
43584378 return ;
43594379 }
4380+
4381+ // We print the address space of the call if it is non-zero.
4382+ // We also print it if it is zero but not equal to the program address space
4383+ // or if we can't find a valid Module* to make it possible to parse
4384+ // the resulting file even without a datalayout string.
43604385 unsigned CallAddrSpace = Operand->getType ()->getPointerAddressSpace ();
4361- bool PrintAddrSpace = CallAddrSpace != 0 ;
4362- if (!PrintAddrSpace) {
4363- const Module *Mod = getModuleFromVal (I);
4364- // We also print it if it is zero but not equal to the program address space
4365- // or if we can't find a valid Module* to make it possible to parse
4366- // the resulting file even without a datalayout string.
4367- if (!Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 )
4368- PrintAddrSpace = true ;
4369- }
4370- if (PrintAddrSpace)
4371- Out << " addrspace(" << CallAddrSpace << " )" ;
4386+ const Module *Mod = getModuleFromVal (I);
4387+ bool ForcePrintAddrSpace =
4388+ !Mod || Mod->getDataLayout ().getProgramAddressSpace () != 0 ;
4389+ printAddressSpace (Mod, CallAddrSpace, Out, /* Prefix=*/ " " , /* Suffix=*/ " " ,
4390+ ForcePrintAddrSpace);
43724391}
43734392
43744393// This member is called for each Instruction in a function..
@@ -4735,9 +4754,8 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
47354754 Out << " , align " << A->value ();
47364755 }
47374756
4738- unsigned AddrSpace = AI->getAddressSpace ();
4739- if (AddrSpace != 0 )
4740- Out << " , addrspace(" << AddrSpace << ' )' ;
4757+ printAddressSpace (AI->getModule (), AI->getAddressSpace (), Out,
4758+ /* Prefix=*/ " , " );
47414759 } else if (isa<CastInst>(I)) {
47424760 if (Operand) {
47434761 Out << ' ' ;
0 commit comments