[llvm] r275676 - Re-commit [AMDGPU] Add metadata for runtime

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 18 17:07:13 PDT 2016


On 07/18/2016 04:52 PM, Matt Arsenault via llvm-commits wrote:
> On 07/15/2016 10:09 PM, Yaxun Liu via llvm-commits wrote:
>> Author: yaxunl
>> Date: Sat Jul 16 00:09:21 2016
>> New Revision: 275676
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=275676&view=rev
>> Log:
>> Re-commit [AMDGPU] Add metadata for runtime
>>
>> Attempting to fix lit test failure on ppc.
>>
>>
>> Added:
>>      llvm/trunk/lib/Target/AMDGPU/AMDGPURuntimeMetadata.h
>>      llvm/trunk/test/CodeGen/AMDGPU/runtime-metadata.ll
>> Modified:
>>      llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
>>      llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.h
>>
>> Modified: llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
>> URL: 
>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp?rev=275676&r1=275675&r2=275676&view=diff
>> ============================================================================== 
>>
>> --- llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp (original)
>> +++ llvm/trunk/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp Sat Jul 16 
>> 00:09:21 2016
>> @@ -39,7 +39,9 @@
>>   #include "llvm/Support/MathExtras.h"
>>   #include "llvm/Support/TargetRegistry.h"
>>   #include "llvm/Target/TargetLoweringObjectFile.h"
>> +#include "AMDGPURuntimeMetadata.h"
>>   +using namespace ::AMDGPU;
>>   using namespace llvm;
>>     // TODO: This should get the default rounding mode from the 
>> kernel. We just set
>> @@ -111,6 +113,7 @@ void AMDGPUAsmPrinter::EmitStartOfAsmFil
>>     AMDGPU::IsaVersion ISA = 
>> AMDGPU::getIsaVersion(STI->getFeatureBits());
>>     TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, 
>> ISA.Stepping,
>>                                       "AMD", "AMDGPU");
>> +  emitStartOfRuntimeMetadata(M);
>>   }
>>     void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
>> @@ -244,6 +247,8 @@ bool AMDGPUAsmPrinter::runOnMachineFunct
>>       }
>>     }
>>   +  emitRuntimeMetadata(*MF.getFunction());
>> +
>>     return false;
>>   }
>>   @@ -740,3 +745,227 @@ bool AMDGPUAsmPrinter::PrintAsmOperand(c
>> *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
>>     return false;
>>   }
>> +
>> +// Emit a key and an integer value for runtime metadata.
>> +static void emitRuntimeMDIntValue(std::unique_ptr<MCStreamer> 
>> &Streamer,
>> +                                  RuntimeMD::Key K, uint64_t V,
>> +                                  unsigned Size) {
>> +  Streamer->EmitIntValue(K, 1);
>> +  Streamer->EmitIntValue(V, Size);
>> +}
>> +
>> +// Emit a key and a string value for runtime metadata.
>> +static void emitRuntimeMDStringValue(std::unique_ptr<MCStreamer> 
>> &Streamer,
>> +                                     RuntimeMD::Key K, StringRef S) {
>> +  Streamer->EmitIntValue(K, 1);
>> +  Streamer->EmitIntValue(S.size(), 4);
>> +  Streamer->EmitBytes(S);
>> +}
>> +
>> +// Emit a key and three integer values for runtime metadata.
>> +// The three integer values are obtained from MDNode \p Node;
>> +static void emitRuntimeMDThreeIntValues(std::unique_ptr<MCStreamer> 
>> &Streamer,
>> +                                        RuntimeMD::Key K, MDNode *Node,
>> +                                        unsigned Size) {
>> +  Streamer->EmitIntValue(K, 1);
>> + Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
>> +    Node->getOperand(0))->getZExtValue(), Size);
>> + Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
>> +    Node->getOperand(1))->getZExtValue(), Size);
>> + Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
>> +    Node->getOperand(2))->getZExtValue(), Size);
>> +}
>> +
>> +void AMDGPUAsmPrinter::emitStartOfRuntimeMetadata(const Module &M) {
>> + OutStreamer->SwitchSection(getObjFileLowering().getContext()
>> +    .getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0));
>> +
>> +  emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyMDVersion,
>> +                        RuntimeMD::MDVersion << 8 | 
>> RuntimeMD::MDRevision, 2);
>> +  if (auto MD = M.getNamedMetadata("opencl.ocl.version")) {
>> +    emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage,
>> +                          RuntimeMD::OpenCL_C, 1);
>> +    auto Node = MD->getOperand(0);
>> +    unsigned short Major = 
>> mdconst::extract<ConstantInt>(Node->getOperand(0))
>> +                             ->getZExtValue();
>> +    unsigned short Minor = 
>> mdconst::extract<ConstantInt>(Node->getOperand(1))
>> +                             ->getZExtValue();
>> +    emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion,
>> +                          Major * 100 + Minor * 10, 2);
>> +  }
>> +}
>> +
>> +static std::string getOCLTypeName(Type *Ty, bool isSigned) {
>> +  if (VectorType* VecTy = dyn_cast<VectorType>(Ty)) {
>> +    Type* EleTy = VecTy->getElementType();
>> +    unsigned Size = VecTy->getVectorNumElements();
>> +    return (Twine(getOCLTypeName(EleTy, isSigned)) + 
>> Twine(Size)).str();
>> +  }
>> +  switch (Ty->getTypeID()) {
>> +  case Type::HalfTyID:   return "half";
>> +  case Type::FloatTyID:  return "float";
>> +  case Type::DoubleTyID: return "double";
>> +  case Type::IntegerTyID: {
>> +    if (!isSigned)
>> +      return (Twine('u') + Twine(getOCLTypeName(Ty, true))).str();
>> +    auto IntTy = cast<IntegerType>(Ty);
>> +    auto BW = IntTy->getIntegerBitWidth();
>> +    switch (BW) {
>> +    case 8:
>> +      return "char";
>> +    case 16:
>> +      return "short";
>> +    case 32:
>> +      return "int";
>> +    case 64:
>> +      return "long";
>> +    default:
>> +      return (Twine('i') + Twine(BW)).str();
>> +    }
>> +  }
>> +  default:
>> +    llvm_unreachable("invalid type");
>> +  }
>> +}
> There are missing tests for some of these still:
> http://llvm.org/reports/coverage/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp.gcov.html 
>
>
> half, float, double, char, short, long, and other bitwidth aren't hit.
>
> We should also be able to switch over every type ID and handle them, 
> so the unreachable should be a warning unless it is after the switch. 
> Array types at least are not handled so need a test
>
> -Matt
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits

I was also thinking we should maybe do this in clang instead and 
directly emit a global with the specified section. I want to eventually 
turn kernel arguments directly into loads off of the kernarg segment 
pointer as part of the calling convention emission, so then the IR 
arguments won't be available for inspection. Plus this really is 
frontend information

-Matt


More information about the llvm-commits mailing list