Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 1 | //===--- AMDGPUCodeObjectMetadataStreamer.cpp -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief AMDGPU Code Object Metadata Streamer. |
| 12 | /// |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "AMDGPU.h" |
| 17 | #include "AMDGPUCodeObjectMetadataStreamer.h" |
| 18 | #include "Utils/AMDGPUBaseInfo.h" |
| 19 | #include "llvm/ADT/StringSwitch.h" |
| 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/YAMLTraits.h" |
| 23 | |
| 24 | using namespace llvm::AMDGPU; |
| 25 | using namespace llvm::AMDGPU::CodeObject; |
| 26 | using namespace llvm::AMDGPU::IsaInfo; |
| 27 | |
| 28 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t) |
| 29 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(std::string) |
| 30 | LLVM_YAML_IS_SEQUENCE_VECTOR(Kernel::Arg::Metadata) |
| 31 | LLVM_YAML_IS_SEQUENCE_VECTOR(Kernel::Metadata) |
| 32 | |
| 33 | namespace llvm { |
| 34 | |
| 35 | static cl::opt<bool> DumpCodeObjectMetadata( |
| 36 | "amdgpu-dump-comd", |
| 37 | cl::desc("Dump AMDGPU Code Object Metadata")); |
| 38 | static cl::opt<bool> VerifyCodeObjectMetadata( |
| 39 | "amdgpu-verify-comd", |
| 40 | cl::desc("Verify AMDGPU Code Object Metadata")); |
| 41 | |
| 42 | namespace yaml { |
| 43 | |
| 44 | template <> |
| 45 | struct ScalarEnumerationTraits<AccessQualifier> { |
| 46 | static void enumeration(IO &YIO, AccessQualifier &EN) { |
| 47 | YIO.enumCase(EN, "Default", AccessQualifier::Default); |
| 48 | YIO.enumCase(EN, "ReadOnly", AccessQualifier::ReadOnly); |
| 49 | YIO.enumCase(EN, "WriteOnly", AccessQualifier::WriteOnly); |
| 50 | YIO.enumCase(EN, "ReadWrite", AccessQualifier::ReadWrite); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | template <> |
| 55 | struct ScalarEnumerationTraits<AddressSpaceQualifier> { |
| 56 | static void enumeration(IO &YIO, AddressSpaceQualifier &EN) { |
| 57 | YIO.enumCase(EN, "Private", AddressSpaceQualifier::Private); |
| 58 | YIO.enumCase(EN, "Global", AddressSpaceQualifier::Global); |
| 59 | YIO.enumCase(EN, "Constant", AddressSpaceQualifier::Constant); |
| 60 | YIO.enumCase(EN, "Local", AddressSpaceQualifier::Local); |
| 61 | YIO.enumCase(EN, "Generic", AddressSpaceQualifier::Generic); |
| 62 | YIO.enumCase(EN, "Region", AddressSpaceQualifier::Region); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | template <> |
| 67 | struct ScalarEnumerationTraits<ValueKind> { |
| 68 | static void enumeration(IO &YIO, ValueKind &EN) { |
| 69 | YIO.enumCase(EN, "ByValue", ValueKind::ByValue); |
| 70 | YIO.enumCase(EN, "GlobalBuffer", ValueKind::GlobalBuffer); |
| 71 | YIO.enumCase(EN, "DynamicSharedPointer", ValueKind::DynamicSharedPointer); |
| 72 | YIO.enumCase(EN, "Sampler", ValueKind::Sampler); |
| 73 | YIO.enumCase(EN, "Image", ValueKind::Image); |
| 74 | YIO.enumCase(EN, "Pipe", ValueKind::Pipe); |
| 75 | YIO.enumCase(EN, "Queue", ValueKind::Queue); |
| 76 | YIO.enumCase(EN, "HiddenGlobalOffsetX", ValueKind::HiddenGlobalOffsetX); |
| 77 | YIO.enumCase(EN, "HiddenGlobalOffsetY", ValueKind::HiddenGlobalOffsetY); |
| 78 | YIO.enumCase(EN, "HiddenGlobalOffsetZ", ValueKind::HiddenGlobalOffsetZ); |
| 79 | YIO.enumCase(EN, "HiddenNone", ValueKind::HiddenNone); |
| 80 | YIO.enumCase(EN, "HiddenPrintfBuffer", ValueKind::HiddenPrintfBuffer); |
| 81 | YIO.enumCase(EN, "HiddenDefaultQueue", ValueKind::HiddenDefaultQueue); |
| 82 | YIO.enumCase(EN, "HiddenCompletionAction", |
| 83 | ValueKind::HiddenCompletionAction); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | template <> |
| 88 | struct ScalarEnumerationTraits<ValueType> { |
| 89 | static void enumeration(IO &YIO, ValueType &EN) { |
| 90 | YIO.enumCase(EN, "Struct", ValueType::Struct); |
| 91 | YIO.enumCase(EN, "I8", ValueType::I8); |
| 92 | YIO.enumCase(EN, "U8", ValueType::U8); |
| 93 | YIO.enumCase(EN, "I16", ValueType::I16); |
| 94 | YIO.enumCase(EN, "U16", ValueType::U16); |
| 95 | YIO.enumCase(EN, "F16", ValueType::F16); |
| 96 | YIO.enumCase(EN, "I32", ValueType::I32); |
| 97 | YIO.enumCase(EN, "U32", ValueType::U32); |
| 98 | YIO.enumCase(EN, "F32", ValueType::F32); |
| 99 | YIO.enumCase(EN, "I64", ValueType::I64); |
| 100 | YIO.enumCase(EN, "U64", ValueType::U64); |
| 101 | YIO.enumCase(EN, "F64", ValueType::F64); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | template <> |
| 106 | struct MappingTraits<Isa::Metadata> { |
| 107 | static void mapping(IO &YIO, Isa::Metadata &MD) { |
| 108 | YIO.mapRequired(Isa::Key::WavefrontSize, MD.mWavefrontSize); |
| 109 | YIO.mapRequired(Isa::Key::LocalMemorySize, MD.mLocalMemorySize); |
| 110 | YIO.mapRequired(Isa::Key::EUsPerCU, MD.mEUsPerCU); |
| 111 | YIO.mapRequired(Isa::Key::MaxWavesPerEU, MD.mMaxWavesPerEU); |
| 112 | YIO.mapRequired(Isa::Key::MaxFlatWorkGroupSize, MD.mMaxFlatWorkGroupSize); |
| 113 | YIO.mapRequired(Isa::Key::SGPRAllocGranule, MD.mSGPRAllocGranule); |
| 114 | YIO.mapRequired(Isa::Key::TotalNumSGPRs, MD.mTotalNumSGPRs); |
| 115 | YIO.mapRequired(Isa::Key::AddressableNumSGPRs, MD.mAddressableNumSGPRs); |
| 116 | YIO.mapRequired(Isa::Key::VGPRAllocGranule, MD.mVGPRAllocGranule); |
| 117 | YIO.mapRequired(Isa::Key::TotalNumVGPRs, MD.mTotalNumVGPRs); |
| 118 | YIO.mapRequired(Isa::Key::AddressableNumVGPRs, MD.mAddressableNumVGPRs); |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | template <> |
| 123 | struct MappingTraits<Kernel::Attrs::Metadata> { |
| 124 | static void mapping(IO &YIO, Kernel::Attrs::Metadata &MD) { |
| 125 | YIO.mapOptional(Kernel::Attrs::Key::ReqdWorkGroupSize, |
| 126 | MD.mReqdWorkGroupSize, std::vector<uint32_t>()); |
| 127 | YIO.mapOptional(Kernel::Attrs::Key::WorkGroupSizeHint, |
| 128 | MD.mWorkGroupSizeHint, std::vector<uint32_t>()); |
| 129 | YIO.mapOptional(Kernel::Attrs::Key::VecTypeHint, |
| 130 | MD.mVecTypeHint, std::string()); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | template <> |
| 135 | struct MappingTraits<Kernel::Arg::Metadata> { |
| 136 | static void mapping(IO &YIO, Kernel::Arg::Metadata &MD) { |
| 137 | YIO.mapRequired(Kernel::Arg::Key::Size, MD.mSize); |
| 138 | YIO.mapRequired(Kernel::Arg::Key::Align, MD.mAlign); |
| 139 | YIO.mapRequired(Kernel::Arg::Key::Kind, MD.mValueKind); |
| 140 | YIO.mapRequired(Kernel::Arg::Key::ValueType, MD.mValueType); |
| 141 | YIO.mapOptional(Kernel::Arg::Key::PointeeAlign, MD.mPointeeAlign, |
| 142 | uint32_t(0)); |
| 143 | YIO.mapOptional(Kernel::Arg::Key::AccQual, MD.mAccQual, |
| 144 | AccessQualifier::Unknown); |
| 145 | YIO.mapOptional(Kernel::Arg::Key::AddrSpaceQual, MD.mAddrSpaceQual, |
| 146 | AddressSpaceQualifier::Unknown); |
| 147 | YIO.mapOptional(Kernel::Arg::Key::IsConst, MD.mIsConst, false); |
| 148 | YIO.mapOptional(Kernel::Arg::Key::IsPipe, MD.mIsPipe, false); |
| 149 | YIO.mapOptional(Kernel::Arg::Key::IsRestrict, MD.mIsRestrict, false); |
| 150 | YIO.mapOptional(Kernel::Arg::Key::IsVolatile, MD.mIsVolatile, false); |
| 151 | YIO.mapOptional(Kernel::Arg::Key::Name, MD.mName, std::string()); |
| 152 | YIO.mapOptional(Kernel::Arg::Key::TypeName, MD.mTypeName, std::string()); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | template <> |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 157 | struct MappingTraits<Kernel::CodeProps::Metadata> { |
| 158 | static void mapping(IO &YIO, Kernel::CodeProps::Metadata &MD) { |
| 159 | YIO.mapOptional(Kernel::CodeProps::Key::KernargSegmentSize, |
| 160 | MD.mKernargSegmentSize, uint64_t(0)); |
| 161 | YIO.mapOptional(Kernel::CodeProps::Key::WorkgroupGroupSegmentSize, |
| 162 | MD.mWorkgroupGroupSegmentSize, uint32_t(0)); |
| 163 | YIO.mapOptional(Kernel::CodeProps::Key::WorkitemPrivateSegmentSize, |
| 164 | MD.mWorkitemPrivateSegmentSize, uint32_t(0)); |
| 165 | YIO.mapOptional(Kernel::CodeProps::Key::WavefrontNumSGPRs, |
| 166 | MD.mWavefrontNumSGPRs, uint16_t(0)); |
| 167 | YIO.mapOptional(Kernel::CodeProps::Key::WorkitemNumVGPRs, |
| 168 | MD.mWorkitemNumVGPRs, uint16_t(0)); |
| 169 | YIO.mapOptional(Kernel::CodeProps::Key::KernargSegmentAlign, |
| 170 | MD.mKernargSegmentAlign, uint8_t(0)); |
| 171 | YIO.mapOptional(Kernel::CodeProps::Key::GroupSegmentAlign, |
| 172 | MD.mGroupSegmentAlign, uint8_t(0)); |
| 173 | YIO.mapOptional(Kernel::CodeProps::Key::PrivateSegmentAlign, |
| 174 | MD.mPrivateSegmentAlign, uint8_t(0)); |
| 175 | YIO.mapOptional(Kernel::CodeProps::Key::WavefrontSize, |
| 176 | MD.mWavefrontSize, uint8_t(0)); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | template <> |
Konstantin Zhuravlyov | a780ffa | 2017-03-22 23:10:46 +0000 | [diff] [blame] | 181 | struct MappingTraits<Kernel::DebugProps::Metadata> { |
| 182 | static void mapping(IO &YIO, Kernel::DebugProps::Metadata &MD) { |
| 183 | YIO.mapOptional(Kernel::DebugProps::Key::DebuggerABIVersion, |
| 184 | MD.mDebuggerABIVersion, std::vector<uint32_t>()); |
| 185 | YIO.mapOptional(Kernel::DebugProps::Key::ReservedNumVGPRs, |
| 186 | MD.mReservedNumVGPRs, uint16_t(0)); |
| 187 | YIO.mapOptional(Kernel::DebugProps::Key::ReservedFirstVGPR, |
| 188 | MD.mReservedFirstVGPR, uint16_t(-1)); |
| 189 | YIO.mapOptional(Kernel::DebugProps::Key::PrivateSegmentBufferSGPR, |
| 190 | MD.mPrivateSegmentBufferSGPR, uint16_t(-1)); |
| 191 | YIO.mapOptional(Kernel::DebugProps::Key::WavefrontPrivateSegmentOffsetSGPR, |
| 192 | MD.mWavefrontPrivateSegmentOffsetSGPR, uint16_t(-1)); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | template <> |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 197 | struct MappingTraits<Kernel::Metadata> { |
| 198 | static void mapping(IO &YIO, Kernel::Metadata &MD) { |
| 199 | YIO.mapRequired(Kernel::Key::Name, MD.mName); |
| 200 | YIO.mapOptional(Kernel::Key::Language, MD.mLanguage, std::string()); |
| 201 | YIO.mapOptional(Kernel::Key::LanguageVersion, MD.mLanguageVersion, |
| 202 | std::vector<uint32_t>()); |
| 203 | if (!MD.mAttrs.empty() || !YIO.outputting()) |
| 204 | YIO.mapOptional(Kernel::Key::Attrs, MD.mAttrs); |
| 205 | if (!MD.mArgs.empty() || !YIO.outputting()) |
| 206 | YIO.mapOptional(Kernel::Key::Args, MD.mArgs); |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 207 | if (!MD.mCodeProps.empty() || !YIO.outputting()) |
| 208 | YIO.mapOptional(Kernel::Key::CodeProps, MD.mCodeProps); |
Konstantin Zhuravlyov | a780ffa | 2017-03-22 23:10:46 +0000 | [diff] [blame] | 209 | if (!MD.mDebugProps.empty() || !YIO.outputting()) |
| 210 | YIO.mapOptional(Kernel::Key::DebugProps, MD.mDebugProps); |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 211 | } |
| 212 | }; |
| 213 | |
| 214 | template <> |
| 215 | struct MappingTraits<CodeObject::Metadata> { |
| 216 | static void mapping(IO &YIO, CodeObject::Metadata &MD) { |
| 217 | YIO.mapRequired(Key::Version, MD.mVersion); |
| 218 | YIO.mapOptional(Key::Isa, MD.mIsa); |
| 219 | YIO.mapOptional(Key::Printf, MD.mPrintf, std::vector<std::string>()); |
| 220 | if (!MD.mKernels.empty() || !YIO.outputting()) |
| 221 | YIO.mapOptional(Key::Kernels, MD.mKernels); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | } // end namespace yaml |
| 226 | |
| 227 | namespace AMDGPU { |
| 228 | |
| 229 | /* static */ |
| 230 | std::error_code CodeObject::Metadata::fromYamlString( |
| 231 | std::string YamlString, CodeObject::Metadata &CodeObjectMetadata) { |
| 232 | yaml::Input YamlInput(YamlString); |
| 233 | YamlInput >> CodeObjectMetadata; |
| 234 | return YamlInput.error(); |
| 235 | } |
| 236 | |
| 237 | /* static */ |
| 238 | std::error_code CodeObject::Metadata::toYamlString( |
| 239 | CodeObject::Metadata CodeObjectMetadata, std::string &YamlString) { |
| 240 | raw_string_ostream YamlStream(YamlString); |
| 241 | yaml::Output YamlOutput(YamlStream, nullptr, std::numeric_limits<int>::max()); |
| 242 | YamlOutput << CodeObjectMetadata; |
| 243 | return std::error_code(); |
| 244 | } |
| 245 | |
| 246 | namespace CodeObject { |
| 247 | |
| 248 | void MetadataStreamer::dump(StringRef YamlString) const { |
| 249 | errs() << "AMDGPU Code Object Metadata:\n" << YamlString << '\n'; |
| 250 | } |
| 251 | |
| 252 | void MetadataStreamer::verify(StringRef YamlString) const { |
| 253 | errs() << "AMDGPU Code Object Metadata Parser Test: "; |
| 254 | |
| 255 | CodeObject::Metadata FromYamlString; |
| 256 | if (Metadata::fromYamlString(YamlString, FromYamlString)) { |
| 257 | errs() << "FAIL\n"; |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | std::string ToYamlString; |
| 262 | if (Metadata::toYamlString(FromYamlString, ToYamlString)) { |
| 263 | errs() << "FAIL\n"; |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | errs() << (YamlString == ToYamlString ? "PASS" : "FAIL") << '\n'; |
| 268 | if (YamlString != ToYamlString) { |
| 269 | errs() << "Original input: " << YamlString << '\n' |
| 270 | << "Produced output: " << ToYamlString << '\n'; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | AccessQualifier MetadataStreamer::getAccessQualifier(StringRef AccQual) const { |
| 275 | if (AccQual.empty()) |
| 276 | return AccessQualifier::Unknown; |
| 277 | |
| 278 | return StringSwitch<AccessQualifier>(AccQual) |
| 279 | .Case("read_only", AccessQualifier::ReadOnly) |
| 280 | .Case("write_only", AccessQualifier::WriteOnly) |
| 281 | .Case("read_write", AccessQualifier::ReadWrite) |
| 282 | .Default(AccessQualifier::Default); |
| 283 | } |
| 284 | |
| 285 | AddressSpaceQualifier MetadataStreamer::getAddressSpaceQualifer( |
| 286 | unsigned AddressSpace) const { |
| 287 | switch (AddressSpace) { |
| 288 | case AMDGPUAS::PRIVATE_ADDRESS: |
| 289 | return AddressSpaceQualifier::Private; |
| 290 | case AMDGPUAS::GLOBAL_ADDRESS: |
| 291 | return AddressSpaceQualifier::Global; |
| 292 | case AMDGPUAS::CONSTANT_ADDRESS: |
| 293 | return AddressSpaceQualifier::Constant; |
| 294 | case AMDGPUAS::LOCAL_ADDRESS: |
| 295 | return AddressSpaceQualifier::Local; |
| 296 | case AMDGPUAS::FLAT_ADDRESS: |
| 297 | return AddressSpaceQualifier::Generic; |
| 298 | case AMDGPUAS::REGION_ADDRESS: |
| 299 | return AddressSpaceQualifier::Region; |
| 300 | } |
| 301 | |
| 302 | llvm_unreachable("Unknown address space qualifier"); |
| 303 | } |
| 304 | |
| 305 | ValueKind MetadataStreamer::getValueKind(Type *Ty, StringRef TypeQual, |
| 306 | StringRef BaseTypeName) const { |
| 307 | if (TypeQual.find("pipe") != StringRef::npos) |
| 308 | return ValueKind::Pipe; |
| 309 | |
| 310 | return StringSwitch<ValueKind>(BaseTypeName) |
| 311 | .Case("sampler_t", ValueKind::Sampler) |
| 312 | .Case("queue_t", ValueKind::Queue) |
| 313 | .Cases("image1d_t", |
| 314 | "image1d_array_t", |
| 315 | "image1d_buffer_t", |
| 316 | "image2d_t" , |
| 317 | "image2d_array_t", |
| 318 | "image2d_array_depth_t", |
| 319 | "image2d_array_msaa_t" |
| 320 | "image2d_array_msaa_depth_t" |
| 321 | "image2d_depth_t", |
| 322 | "image2d_msaa_t", |
| 323 | "image2d_msaa_depth_t", |
| 324 | "image3d_t", ValueKind::Image) |
| 325 | .Default(isa<PointerType>(Ty) ? |
| 326 | (Ty->getPointerAddressSpace() == |
| 327 | AMDGPUAS::LOCAL_ADDRESS ? |
| 328 | ValueKind::DynamicSharedPointer : |
| 329 | ValueKind::GlobalBuffer) : |
| 330 | ValueKind::ByValue); |
| 331 | } |
| 332 | |
| 333 | ValueType MetadataStreamer::getValueType(Type *Ty, StringRef TypeName) const { |
| 334 | switch (Ty->getTypeID()) { |
| 335 | case Type::IntegerTyID: { |
| 336 | auto Signed = !TypeName.startswith("u"); |
| 337 | switch (Ty->getIntegerBitWidth()) { |
| 338 | case 8: |
| 339 | return Signed ? ValueType::I8 : ValueType::U8; |
| 340 | case 16: |
| 341 | return Signed ? ValueType::I16 : ValueType::U16; |
| 342 | case 32: |
| 343 | return Signed ? ValueType::I32 : ValueType::U32; |
| 344 | case 64: |
| 345 | return Signed ? ValueType::I64 : ValueType::U64; |
| 346 | default: |
| 347 | return ValueType::Struct; |
| 348 | } |
| 349 | } |
| 350 | case Type::HalfTyID: |
| 351 | return ValueType::F16; |
| 352 | case Type::FloatTyID: |
| 353 | return ValueType::F32; |
| 354 | case Type::DoubleTyID: |
| 355 | return ValueType::F64; |
| 356 | case Type::PointerTyID: |
| 357 | return getValueType(Ty->getPointerElementType(), TypeName); |
| 358 | case Type::VectorTyID: |
| 359 | return getValueType(Ty->getVectorElementType(), TypeName); |
| 360 | default: |
| 361 | return ValueType::Struct; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | std::string MetadataStreamer::getTypeName(Type *Ty, bool Signed) const { |
| 366 | switch (Ty->getTypeID()) { |
| 367 | case Type::IntegerTyID: { |
| 368 | if (!Signed) |
| 369 | return (Twine('u') + getTypeName(Ty, true)).str(); |
| 370 | |
| 371 | auto BitWidth = Ty->getIntegerBitWidth(); |
| 372 | switch (BitWidth) { |
| 373 | case 8: |
| 374 | return "char"; |
| 375 | case 16: |
| 376 | return "short"; |
| 377 | case 32: |
| 378 | return "int"; |
| 379 | case 64: |
| 380 | return "long"; |
| 381 | default: |
| 382 | return (Twine('i') + Twine(BitWidth)).str(); |
| 383 | } |
| 384 | } |
| 385 | case Type::HalfTyID: |
| 386 | return "half"; |
| 387 | case Type::FloatTyID: |
| 388 | return "float"; |
| 389 | case Type::DoubleTyID: |
| 390 | return "double"; |
| 391 | case Type::VectorTyID: { |
| 392 | auto VecTy = cast<VectorType>(Ty); |
| 393 | auto ElTy = VecTy->getElementType(); |
| 394 | auto NumElements = VecTy->getVectorNumElements(); |
| 395 | return (Twine(getTypeName(ElTy, Signed)) + Twine(NumElements)).str(); |
| 396 | } |
| 397 | default: |
| 398 | return "unknown"; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | std::vector<uint32_t> MetadataStreamer::getWorkGroupDimensions( |
| 403 | MDNode *Node) const { |
| 404 | std::vector<uint32_t> Dims; |
| 405 | if (Node->getNumOperands() != 3) |
| 406 | return Dims; |
| 407 | |
| 408 | for (auto &Op : Node->operands()) |
| 409 | Dims.push_back(mdconst::extract<ConstantInt>(Op)->getZExtValue()); |
| 410 | return Dims; |
| 411 | } |
| 412 | |
| 413 | void MetadataStreamer::emitVersion() { |
| 414 | auto &Version = CodeObjectMetadata.mVersion; |
| 415 | |
| 416 | Version.push_back(MetadataVersionMajor); |
| 417 | Version.push_back(MetadataVersionMinor); |
| 418 | } |
| 419 | |
| 420 | void MetadataStreamer::emitIsa(const FeatureBitset &Features) { |
| 421 | auto &Isa = CodeObjectMetadata.mIsa; |
| 422 | |
| 423 | Isa.mWavefrontSize = getWavefrontSize(Features); |
| 424 | Isa.mLocalMemorySize = getLocalMemorySize(Features); |
| 425 | Isa.mEUsPerCU = getEUsPerCU(Features); |
| 426 | Isa.mMaxWavesPerEU = getMaxWavesPerEU(Features); |
| 427 | Isa.mMaxFlatWorkGroupSize = getMaxFlatWorkGroupSize(Features); |
| 428 | Isa.mSGPRAllocGranule = getSGPRAllocGranule(Features); |
| 429 | Isa.mTotalNumSGPRs = getTotalNumSGPRs(Features); |
| 430 | Isa.mAddressableNumSGPRs = getAddressableNumSGPRs(Features); |
| 431 | Isa.mVGPRAllocGranule = getVGPRAllocGranule(Features); |
| 432 | Isa.mTotalNumVGPRs = getTotalNumVGPRs(Features); |
| 433 | Isa.mAddressableNumVGPRs = getAddressableNumVGPRs(Features); |
| 434 | } |
| 435 | |
| 436 | void MetadataStreamer::emitPrintf(const Module &Mod) { |
| 437 | auto &Printf = CodeObjectMetadata.mPrintf; |
| 438 | |
| 439 | auto Node = Mod.getNamedMetadata("llvm.printf.fmts"); |
| 440 | if (!Node) |
| 441 | return; |
| 442 | |
| 443 | for (auto Op : Node->operands()) |
| 444 | if (Op->getNumOperands()) |
| 445 | Printf.push_back(cast<MDString>(Op->getOperand(0))->getString()); |
| 446 | } |
| 447 | |
| 448 | void MetadataStreamer::emitKernelLanguage(const Function &Func) { |
| 449 | auto &Kernel = CodeObjectMetadata.mKernels.back(); |
| 450 | |
| 451 | // TODO: What about other languages? |
| 452 | auto Node = Func.getParent()->getNamedMetadata("opencl.ocl.version"); |
| 453 | if (!Node || !Node->getNumOperands()) |
| 454 | return; |
| 455 | auto Op0 = Node->getOperand(0); |
| 456 | if (Op0->getNumOperands() <= 1) |
| 457 | return; |
| 458 | |
| 459 | Kernel.mLanguage = "OpenCL C"; |
| 460 | Kernel.mLanguageVersion.push_back( |
| 461 | mdconst::extract<ConstantInt>(Op0->getOperand(0))->getZExtValue()); |
| 462 | Kernel.mLanguageVersion.push_back( |
| 463 | mdconst::extract<ConstantInt>(Op0->getOperand(1))->getZExtValue()); |
| 464 | } |
| 465 | |
| 466 | void MetadataStreamer::emitKernelAttrs(const Function &Func) { |
| 467 | auto &Attrs = CodeObjectMetadata.mKernels.back().mAttrs; |
| 468 | |
| 469 | if (auto Node = Func.getMetadata("reqd_work_group_size")) |
| 470 | Attrs.mReqdWorkGroupSize = getWorkGroupDimensions(Node); |
| 471 | if (auto Node = Func.getMetadata("work_group_size_hint")) |
| 472 | Attrs.mWorkGroupSizeHint = getWorkGroupDimensions(Node); |
| 473 | if (auto Node = Func.getMetadata("vec_type_hint")) { |
| 474 | Attrs.mVecTypeHint = getTypeName( |
| 475 | cast<ValueAsMetadata>(Node->getOperand(0))->getType(), |
| 476 | mdconst::extract<ConstantInt>(Node->getOperand(1))->getZExtValue()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void MetadataStreamer::emitKernelArgs(const Function &Func) { |
| 481 | for (auto &Arg : Func.args()) |
| 482 | emitKernelArg(Arg); |
| 483 | |
| 484 | // TODO: What about other languages? |
| 485 | if (!Func.getParent()->getNamedMetadata("opencl.ocl.version")) |
| 486 | return; |
| 487 | |
| 488 | auto &DL = Func.getParent()->getDataLayout(); |
| 489 | auto Int64Ty = Type::getInt64Ty(Func.getContext()); |
| 490 | |
| 491 | emitKernelArg(DL, Int64Ty, ValueKind::HiddenGlobalOffsetX); |
| 492 | emitKernelArg(DL, Int64Ty, ValueKind::HiddenGlobalOffsetY); |
| 493 | emitKernelArg(DL, Int64Ty, ValueKind::HiddenGlobalOffsetZ); |
| 494 | |
| 495 | if (!Func.getParent()->getNamedMetadata("llvm.printf.fmts")) |
| 496 | return; |
| 497 | |
| 498 | auto Int8PtrTy = Type::getInt8PtrTy(Func.getContext(), |
| 499 | AMDGPUAS::GLOBAL_ADDRESS); |
| 500 | emitKernelArg(DL, Int8PtrTy, ValueKind::HiddenPrintfBuffer); |
| 501 | } |
| 502 | |
| 503 | void MetadataStreamer::emitKernelArg(const Argument &Arg) { |
| 504 | auto Func = Arg.getParent(); |
| 505 | auto ArgNo = Arg.getArgNo(); |
| 506 | const MDNode *Node; |
| 507 | |
| 508 | StringRef TypeQual; |
| 509 | Node = Func->getMetadata("kernel_arg_type_qual"); |
| 510 | if (Node && ArgNo < Node->getNumOperands()) |
| 511 | TypeQual = cast<MDString>(Node->getOperand(ArgNo))->getString(); |
| 512 | |
| 513 | StringRef BaseTypeName; |
| 514 | Node = Func->getMetadata("kernel_arg_base_type"); |
| 515 | if (Node && ArgNo < Node->getNumOperands()) |
| 516 | BaseTypeName = cast<MDString>(Node->getOperand(ArgNo))->getString(); |
| 517 | |
| 518 | StringRef AccQual; |
| 519 | Node = Func->getMetadata("kernel_arg_access_qual"); |
| 520 | if (Node && ArgNo < Node->getNumOperands()) |
| 521 | AccQual = cast<MDString>(Node->getOperand(ArgNo))->getString(); |
| 522 | |
| 523 | StringRef Name; |
| 524 | Node = Func->getMetadata("kernel_arg_name"); |
| 525 | if (Node && ArgNo < Node->getNumOperands()) |
| 526 | Name = cast<MDString>(Node->getOperand(ArgNo))->getString(); |
| 527 | |
| 528 | StringRef TypeName; |
| 529 | Node = Func->getMetadata("kernel_arg_type"); |
| 530 | if (Node && ArgNo < Node->getNumOperands()) |
| 531 | TypeName = cast<MDString>(Node->getOperand(ArgNo))->getString(); |
| 532 | |
| 533 | emitKernelArg(Func->getParent()->getDataLayout(), Arg.getType(), |
| 534 | getValueKind(Arg.getType(), TypeQual, BaseTypeName), TypeQual, |
| 535 | BaseTypeName, AccQual, Name, TypeName); |
| 536 | } |
| 537 | |
| 538 | void MetadataStreamer::emitKernelArg(const DataLayout &DL, Type *Ty, |
| 539 | ValueKind ValueKind, StringRef TypeQual, |
| 540 | StringRef BaseTypeName, StringRef AccQual, |
| 541 | StringRef Name, StringRef TypeName) { |
| 542 | CodeObjectMetadata.mKernels.back().mArgs.push_back(Kernel::Arg::Metadata()); |
| 543 | auto &Arg = CodeObjectMetadata.mKernels.back().mArgs.back(); |
| 544 | |
| 545 | Arg.mSize = DL.getTypeAllocSize(Ty); |
| 546 | Arg.mAlign = DL.getABITypeAlignment(Ty); |
| 547 | Arg.mValueKind = ValueKind; |
| 548 | Arg.mValueType = getValueType(Ty, BaseTypeName); |
| 549 | |
| 550 | if (auto PtrTy = dyn_cast<PointerType>(Ty)) { |
| 551 | auto ElTy = PtrTy->getElementType(); |
| 552 | if (PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && ElTy->isSized()) |
| 553 | Arg.mPointeeAlign = DL.getABITypeAlignment(ElTy); |
| 554 | } |
| 555 | |
| 556 | Arg.mAccQual = getAccessQualifier(AccQual); |
| 557 | |
| 558 | if (auto PtrTy = dyn_cast<PointerType>(Ty)) |
| 559 | Arg.mAddrSpaceQual = getAddressSpaceQualifer(PtrTy->getAddressSpace()); |
| 560 | |
| 561 | SmallVector<StringRef, 1> SplitTypeQuals; |
| 562 | TypeQual.split(SplitTypeQuals, " ", -1, false); |
| 563 | for (StringRef Key : SplitTypeQuals) { |
| 564 | auto P = StringSwitch<bool*>(Key) |
| 565 | .Case("const", &Arg.mIsConst) |
| 566 | .Case("pipe", &Arg.mIsPipe) |
| 567 | .Case("restrict", &Arg.mIsRestrict) |
| 568 | .Case("volatile", &Arg.mIsVolatile) |
| 569 | .Default(nullptr); |
| 570 | if (P) |
| 571 | *P = true; |
| 572 | } |
| 573 | |
| 574 | Arg.mName = Name; |
| 575 | Arg.mTypeName = TypeName; |
| 576 | } |
| 577 | |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 578 | void MetadataStreamer::emitKernelCodeProps( |
| 579 | const amd_kernel_code_t &KernelCode) { |
| 580 | auto &CodeProps = CodeObjectMetadata.mKernels.back().mCodeProps; |
| 581 | |
| 582 | CodeProps.mKernargSegmentSize = KernelCode.kernarg_segment_byte_size; |
| 583 | CodeProps.mWorkgroupGroupSegmentSize = |
| 584 | KernelCode.workgroup_group_segment_byte_size; |
| 585 | CodeProps.mWorkitemPrivateSegmentSize = |
| 586 | KernelCode.workitem_private_segment_byte_size; |
| 587 | CodeProps.mWavefrontNumSGPRs = KernelCode.wavefront_sgpr_count; |
| 588 | CodeProps.mWorkitemNumVGPRs = KernelCode.workitem_vgpr_count; |
| 589 | CodeProps.mKernargSegmentAlign = KernelCode.kernarg_segment_alignment; |
| 590 | CodeProps.mGroupSegmentAlign = KernelCode.group_segment_alignment; |
| 591 | CodeProps.mPrivateSegmentAlign = KernelCode.private_segment_alignment; |
| 592 | CodeProps.mWavefrontSize = KernelCode.wavefront_size; |
| 593 | } |
| 594 | |
Konstantin Zhuravlyov | a780ffa | 2017-03-22 23:10:46 +0000 | [diff] [blame] | 595 | void MetadataStreamer::emitKernelDebugProps( |
| 596 | const amd_kernel_code_t &KernelCode) { |
| 597 | if (!(KernelCode.code_properties & AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED)) |
| 598 | return; |
| 599 | |
| 600 | auto &DebugProps = CodeObjectMetadata.mKernels.back().mDebugProps; |
| 601 | |
| 602 | // FIXME: Need to pass down debugger ABI version through features. This is ok |
| 603 | // for now because we only have one version. |
| 604 | DebugProps.mDebuggerABIVersion.push_back(1); |
| 605 | DebugProps.mDebuggerABIVersion.push_back(0); |
| 606 | DebugProps.mReservedNumVGPRs = KernelCode.reserved_vgpr_count; |
| 607 | DebugProps.mReservedFirstVGPR = KernelCode.reserved_vgpr_first; |
| 608 | DebugProps.mPrivateSegmentBufferSGPR = |
| 609 | KernelCode.debug_private_segment_buffer_sgpr; |
| 610 | DebugProps.mWavefrontPrivateSegmentOffsetSGPR = |
| 611 | KernelCode.debug_wavefront_private_segment_offset_sgpr; |
| 612 | } |
| 613 | |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 614 | void MetadataStreamer::begin(const FeatureBitset &Features, const Module &Mod) { |
| 615 | emitVersion(); |
| 616 | emitIsa(Features); |
| 617 | emitPrintf(Mod); |
| 618 | } |
| 619 | |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 620 | void MetadataStreamer::emitKernel(const Function &Func, |
| 621 | const amd_kernel_code_t &KernelCode) { |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 622 | if (Func.getCallingConv() != CallingConv::AMDGPU_KERNEL) |
| 623 | return; |
| 624 | |
| 625 | CodeObjectMetadata.mKernels.push_back(Kernel::Metadata()); |
| 626 | auto &Kernel = CodeObjectMetadata.mKernels.back(); |
| 627 | |
| 628 | Kernel.mName = Func.getName(); |
| 629 | emitKernelLanguage(Func); |
| 630 | emitKernelAttrs(Func); |
| 631 | emitKernelArgs(Func); |
Konstantin Zhuravlyov | ca0e7f6 | 2017-03-22 22:54:39 +0000 | [diff] [blame] | 632 | emitKernelCodeProps(KernelCode); |
Konstantin Zhuravlyov | a780ffa | 2017-03-22 23:10:46 +0000 | [diff] [blame] | 633 | emitKernelDebugProps(KernelCode); |
Konstantin Zhuravlyov | 7498cd6 | 2017-03-22 22:32:22 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | ErrorOr<std::string> MetadataStreamer::toYamlString() { |
| 637 | std::string YamlString; |
| 638 | if (auto Error = Metadata::toYamlString(CodeObjectMetadata, YamlString)) |
| 639 | return Error; |
| 640 | |
| 641 | if (DumpCodeObjectMetadata) |
| 642 | dump(YamlString); |
| 643 | if (VerifyCodeObjectMetadata) |
| 644 | verify(YamlString); |
| 645 | |
| 646 | return YamlString; |
| 647 | } |
| 648 | |
| 649 | ErrorOr<std::string> MetadataStreamer::toYamlString( |
| 650 | const FeatureBitset &Features, StringRef YamlString) { |
| 651 | if (auto Error = Metadata::fromYamlString(YamlString, CodeObjectMetadata)) |
| 652 | return Error; |
| 653 | |
| 654 | emitIsa(Features); |
| 655 | return toYamlString(); |
| 656 | } |
| 657 | |
| 658 | } // end namespace CodeObject |
| 659 | } // end namespace AMDGPU |
| 660 | } // end namespace llvm |