Commit b0bb2e99 authored by Bill Wendling's avatar Bill Wendling
Browse files

Merging r182254:

------------------------------------------------------------------------
r182254 | jholewinski | 2013-05-20 05:13:32 -0700 (Mon, 20 May 2013) | 12 lines

[NVPTX] Add GenericToNVVM IR converter to better handle idiomatic LLVM IR inputs

This converter currently only handles global variables in address space 0. For
these variables, they are promoted to address space 1 (global memory), and all
uses are updated to point to the result of a cvta.global instruction on the new
variable.

The motivation for this is address space 0 global variables are illegal since we
cannot declare variables in the generic address space.  Instead, we place the
variables in address space 1 and explicitly convert the pointer to address
space 0. This is primarily intended to help new users who expect to be able to
place global variables in the default address space.
------------------------------------------------------------------------

llvm-svn: 182826
parent 55ebf7b0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ set(NVPTXCodeGen_sources
  NVPTXAsmPrinter.cpp
  NVPTXUtilities.cpp
  NVVMReflect.cpp
  NVPTXGenericToNVVM.cpp
  )

add_llvm_target(NVPTXCodeGen ${NVPTXCodeGen_sources})
+1 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ createNVPTXISelDag(NVPTXTargetMachine &TM, llvm::CodeGenOpt::Level OptLevel);
FunctionPass *createLowerStructArgsPass(NVPTXTargetMachine &);
FunctionPass *createNVPTXReMatPass(NVPTXTargetMachine &);
FunctionPass *createNVPTXReMatBlockPass(NVPTXTargetMachine &);
ModulePass *createGenericToNVVMPass();

bool isImageOrSamplerVal(const Value *, const Module *);

+62 −41
Original line number Diff line number Diff line
@@ -68,11 +68,12 @@ InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore,
namespace {
/// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
/// depends.
void DiscoverDependentGlobals(Value *V, DenseSet<GlobalVariable *> &Globals) {
  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
void DiscoverDependentGlobals(const Value *V,
                              DenseSet<const GlobalVariable *> &Globals) {
  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
    Globals.insert(GV);
  else {
    if (User *U = dyn_cast<User>(V)) {
    if (const User *U = dyn_cast<User>(V)) {
      for (unsigned i = 0, e = U->getNumOperands(); i != e; ++i) {
        DiscoverDependentGlobals(U->getOperand(i), Globals);
      }
@@ -84,8 +85,9 @@ void DiscoverDependentGlobals(Value *V, DenseSet<GlobalVariable *> &Globals) {
/// instances to be emitted, but only after any dependents have been added
/// first.
void VisitGlobalVariableForEmission(
    GlobalVariable *GV, SmallVectorImpl<GlobalVariable *> &Order,
    DenseSet<GlobalVariable *> &Visited, DenseSet<GlobalVariable *> &Visiting) {
    const GlobalVariable *GV, SmallVectorImpl<const GlobalVariable *> &Order,
    DenseSet<const GlobalVariable *> &Visited,
    DenseSet<const GlobalVariable *> &Visiting) {
  // Have we already visited this one?
  if (Visited.count(GV))
    return;
@@ -98,11 +100,11 @@ void VisitGlobalVariableForEmission(
  Visiting.insert(GV);

  // Make sure we visit all dependents first
  DenseSet<GlobalVariable *> Others;
  DenseSet<const GlobalVariable *> Others;
  for (unsigned i = 0, e = GV->getNumOperands(); i != e; ++i)
    DiscoverDependentGlobals(GV->getOperand(i), Others);

  for (DenseSet<GlobalVariable *>::iterator I = Others.begin(),
  for (DenseSet<const GlobalVariable *>::iterator I = Others.begin(),
                                                  E = Others.end();
       I != E; ++I)
    VisitGlobalVariableForEmission(*I, Order, Visited, Visiting);
@@ -405,6 +407,11 @@ void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
  SmallString<128> Str;
  raw_svector_ostream O(Str);

  if (!GlobalsEmitted) {
    emitGlobals(*MF->getFunction()->getParent());
    GlobalsEmitted = true;
  }
  
  // Set up
  MRI = &MF->getRegInfo();
  F = MF->getFunction();
@@ -795,7 +802,7 @@ static bool useFuncSeen(const Constant *C,
  return false;
}

void NVPTXAsmPrinter::emitDeclarations(Module &M, raw_ostream &O) {
void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
  llvm::DenseMap<const Function *, bool> seenMap;
  for (Module::const_iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
    const Function *F = FI;
@@ -921,6 +928,12 @@ bool NVPTXAsmPrinter::doInitialization(Module &M) {
  if (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA)
    recordAndEmitFilenames(M);

  GlobalsEmitted = false;
    
  return false; // success
}

void NVPTXAsmPrinter::emitGlobals(const Module &M) {
  SmallString<128> Str2;
  raw_svector_ostream OS2(Str2);

@@ -931,13 +944,13 @@ bool NVPTXAsmPrinter::doInitialization(Module &M) {
  // global variable in order, and ensure that we emit it *after* its dependent
  // globals. We use a little extra memory maintaining both a set and a list to
  // have fast searches while maintaining a strict ordering.
  SmallVector<GlobalVariable *, 8> Globals;
  DenseSet<GlobalVariable *> GVVisited;
  DenseSet<GlobalVariable *> GVVisiting;
  SmallVector<const GlobalVariable *, 8> Globals;
  DenseSet<const GlobalVariable *> GVVisited;
  DenseSet<const GlobalVariable *> GVVisiting;

  // Visit each global variable, in order
  for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;
       ++I)
  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
       I != E; ++I)
    VisitGlobalVariableForEmission(I, Globals, GVVisited, GVVisiting);

  assert(GVVisited.size() == M.getGlobalList().size() &&
@@ -951,7 +964,6 @@ bool NVPTXAsmPrinter::doInitialization(Module &M) {
  OS2 << '\n';

  OutStreamer.EmitRawText(OS2.str());
  return false; // success
}

void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) {
@@ -989,6 +1001,14 @@ void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O) {
}

bool NVPTXAsmPrinter::doFinalization(Module &M) {

  // If we did not emit any functions, then the global declarations have not
  // yet been emitted.
  if (!GlobalsEmitted) {
    emitGlobals(M);
    GlobalsEmitted = true;
  }

  // XXX Temproarily remove global variables so that doFinalization() will not
  // emit them again (global variables are emitted at beginning).

@@ -1063,7 +1083,8 @@ void NVPTXAsmPrinter::emitLinkageDirective(const GlobalValue *V,
  }
}

void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O,
void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar,
                                         raw_ostream &O,
                                         bool processDemoted) {

  // Skip meta data
@@ -1107,10 +1128,10 @@ void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O,
  if (llvm::isSampler(*GVar)) {
    O << ".global .samplerref " << llvm::getSamplerName(*GVar);

    Constant *Initializer = NULL;
    const Constant *Initializer = NULL;
    if (GVar->hasInitializer())
      Initializer = GVar->getInitializer();
    ConstantInt *CI = NULL;
    const ConstantInt *CI = NULL;
    if (Initializer)
      CI = dyn_cast<ConstantInt>(Initializer);
    if (CI) {
@@ -1183,7 +1204,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O,
    if (localDecls.find(demotedFunc) != localDecls.end())
      localDecls[demotedFunc].push_back(GVar);
    else {
      std::vector<GlobalVariable *> temp;
      std::vector<const GlobalVariable *> temp;
      temp.push_back(GVar);
      localDecls[demotedFunc] = temp;
    }
@@ -1213,7 +1234,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O,
         (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) ||
         (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
        GVar->hasInitializer()) {
      Constant *Initializer = GVar->getInitializer();
      const Constant *Initializer = GVar->getInitializer();
      if (!Initializer->isNullValue()) {
        O << " = ";
        printScalarConstant(Initializer, O);
@@ -1237,7 +1258,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O,
           (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST_NOT_GEN) ||
           (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) &&
          GVar->hasInitializer()) {
        Constant *Initializer = GVar->getInitializer();
        const Constant *Initializer = GVar->getInitializer();
        if (!isa<UndefValue>(Initializer) && !Initializer->isNullValue()) {
          AggBuffer aggBuffer(ElementSize, O, *this);
          bufferAggregateConstant(Initializer, &aggBuffer);
@@ -1287,7 +1308,7 @@ void NVPTXAsmPrinter::emitDemotedVars(const Function *f, raw_ostream &O) {
  if (localDecls.find(f) == localDecls.end())
    return;

  std::vector<GlobalVariable *> &gvars = localDecls[f];
  std::vector<const GlobalVariable *> &gvars = localDecls[f];

  for (unsigned i = 0, e = gvars.size(); i != e; ++i) {
    O << "\t// demoted variable\n\t";
@@ -1761,12 +1782,12 @@ void NVPTXAsmPrinter::printFPConstant(const ConstantFP *Fp, raw_ostream &O) {
  O << utohexstr(API.getZExtValue());
}

void NVPTXAsmPrinter::printScalarConstant(Constant *CPV, raw_ostream &O) {
  if (ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
  if (const ConstantInt *CI = dyn_cast<ConstantInt>(CPV)) {
    O << CI->getValue();
    return;
  }
  if (ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
  if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV)) {
    printFPConstant(CFP, O);
    return;
  }
@@ -1774,13 +1795,13 @@ void NVPTXAsmPrinter::printScalarConstant(Constant *CPV, raw_ostream &O) {
    O << "0";
    return;
  }
  if (GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
  if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
    O << *Mang->getSymbol(GVar);
    return;
  }
  if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
    Value *v = Cexpr->stripPointerCasts();
    if (GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
  if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
    const Value *v = Cexpr->stripPointerCasts();
    if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
      O << *Mang->getSymbol(GVar);
      return;
    } else {
@@ -1791,7 +1812,7 @@ void NVPTXAsmPrinter::printScalarConstant(Constant *CPV, raw_ostream &O) {
  llvm_unreachable("Not scalar type found in printScalarConstant()");
}

void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
                                   AggBuffer *aggBuffer) {

  const DataLayout *TD = TM.getDataLayout();
@@ -1819,13 +1840,13 @@ void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
      ptr = (unsigned char *)&int16;
      aggBuffer->addBytes(ptr, 2, Bytes);
    } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
      if (ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
      if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
        int int32 = (int)(constInt->getZExtValue());
        ptr = (unsigned char *)&int32;
        aggBuffer->addBytes(ptr, 4, Bytes);
        break;
      } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
        if (ConstantInt *constInt = dyn_cast<ConstantInt>(
      } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
        if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
                ConstantFoldConstantExpression(Cexpr, TD))) {
          int int32 = (int)(constInt->getZExtValue());
          ptr = (unsigned char *)&int32;
@@ -1841,13 +1862,13 @@ void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
      }
      llvm_unreachable("unsupported integer const type");
    } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
      if (ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
      if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
        long long int64 = (long long)(constInt->getZExtValue());
        ptr = (unsigned char *)&int64;
        aggBuffer->addBytes(ptr, 8, Bytes);
        break;
      } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
        if (ConstantInt *constInt = dyn_cast<ConstantInt>(
      } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
        if (const ConstantInt *constInt = dyn_cast<ConstantInt>(
                ConstantFoldConstantExpression(Cexpr, TD))) {
          long long int64 = (long long)(constInt->getZExtValue());
          ptr = (unsigned char *)&int64;
@@ -1868,7 +1889,7 @@ void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
  }
  case Type::FloatTyID:
  case Type::DoubleTyID: {
    ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
    const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
    const Type *Ty = CFP->getType();
    if (Ty == Type::getFloatTy(CPV->getContext())) {
      float float32 = (float) CFP->getValueAPF().convertToFloat();
@@ -1884,10 +1905,10 @@ void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
    break;
  }
  case Type::PointerTyID: {
    if (GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
    if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
      aggBuffer->addSymbol(GVar);
    } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
      Value *v = Cexpr->stripPointerCasts();
    } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
      const Value *v = Cexpr->stripPointerCasts();
      aggBuffer->addSymbol(v);
    }
    unsigned int s = TD->getTypeAllocSize(CPV->getType());
@@ -1916,7 +1937,7 @@ void NVPTXAsmPrinter::bufferLEByte(Constant *CPV, int Bytes,
  }
}

void NVPTXAsmPrinter::bufferAggregateConstant(Constant *CPV,
void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
                                              AggBuffer *aggBuffer) {
  const DataLayout *TD = TM.getDataLayout();
  int Bytes;
+15 −11
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
    unsigned char *buffer; // the buffer
    unsigned numSymbols;   // number of symbol addresses
    SmallVector<unsigned, 4> symbolPosInBuffer;
    SmallVector<Value *, 4> Symbols;
    SmallVector<const Value *, 4> Symbols;

  private:
    unsigned curpos;
@@ -128,7 +128,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
      }
      return curpos;
    }
    void addSymbol(Value *GVar) {
    void addSymbol(const Value *GVar) {
      symbolPosInBuffer.push_back(curpos);
      Symbols.push_back(GVar);
      numSymbols++;
@@ -153,11 +153,11 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
          if (pos)
            O << ", ";
          if (pos == nextSymbolPos) {
            Value *v = Symbols[nSym];
            if (GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
            const Value *v = Symbols[nSym];
            if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
              MCSymbol *Name = AP.Mang->getSymbol(GVar);
              O << *Name;
            } else if (ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
            } else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
              O << *nvptx::LowerConstant(Cexpr, AP);
            } else
              llvm_unreachable("symbol type unknown");
@@ -205,10 +205,12 @@ private:
  void printImplicitDef(const MachineInstr *MI, raw_ostream &O) const;
  // definition autogenerated.
  void printInstruction(const MachineInstr *MI, raw_ostream &O);
  void printModuleLevelGV(GlobalVariable *GVar, raw_ostream &O, bool = false);
  void printModuleLevelGV(const GlobalVariable *GVar, raw_ostream &O,
                          bool = false);
  void printParamName(int paramIndex, raw_ostream &O);
  void printParamName(Function::const_arg_iterator I, int paramIndex,
                      raw_ostream &O);
  void emitGlobals(const Module &M);
  void emitHeader(Module &M, raw_ostream &O);
  void emitKernelFunctionDirectives(const Function &F, raw_ostream &O) const;
  void emitVirtualRegister(unsigned int vr, bool isVec, raw_ostream &O);
@@ -234,6 +236,8 @@ protected:
private:
  std::string CurrentBankselLabelInBasicBlock;

  bool GlobalsEmitted;
  
  // This is specific per MachineFunction.
  const MachineRegisterInfo *MRI;
  // The contents are specific for each
@@ -247,7 +251,7 @@ private:
  std::map<const Type *, std::string> TypeNameMap;

  // List of variables demoted to a function scope.
  std::map<const Function *, std::vector<GlobalVariable *> > localDecls;
  std::map<const Function *, std::vector<const GlobalVariable *> > localDecls;

  // To record filename to ID mapping
  std::map<std::string, unsigned> filenameMap;
@@ -256,15 +260,15 @@ private:
  void emitPTXGlobalVariable(const GlobalVariable *GVar, raw_ostream &O);
  void emitPTXAddressSpace(unsigned int AddressSpace, raw_ostream &O) const;
  std::string getPTXFundamentalTypeStr(const Type *Ty, bool = true) const;
  void printScalarConstant(Constant *CPV, raw_ostream &O);
  void printScalarConstant(const Constant *CPV, raw_ostream &O);
  void printFPConstant(const ConstantFP *Fp, raw_ostream &O);
  void bufferLEByte(Constant *CPV, int Bytes, AggBuffer *aggBuffer);
  void bufferAggregateConstant(Constant *CV, AggBuffer *aggBuffer);
  void bufferLEByte(const Constant *CPV, int Bytes, AggBuffer *aggBuffer);
  void bufferAggregateConstant(const Constant *CV, AggBuffer *aggBuffer);

  void printOperandProper(const MachineOperand &MO);

  void emitLinkageDirective(const GlobalValue *V, raw_ostream &O);
  void emitDeclarations(Module &, raw_ostream &O);
  void emitDeclarations(const Module &, raw_ostream &O);
  void emitDeclaration(const Function *, raw_ostream &O);

  static const char *getRegisterName(unsigned RegNo);
+436 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading