1 //===- llvm/unittest/CodeGen/AMDGPUMetadataTest.cpp -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// Test that amdgpu metadata that is added in a pass is read by the asm emitter 11 /// and stored in the ELF. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/LegacyPassManager.h" 16 #include "llvm/MC/TargetRegistry.h" 17 #include "llvm/Support/TargetSelect.h" 18 #include "llvm/Target/TargetMachine.h" 19 #include "gtest/gtest.h" 20 21 namespace llvm { 22 23 namespace { 24 // Pass that adds global metadata 25 struct AddMetadataPass : public ModulePass { 26 std::string PalMDString; 27 28 public: 29 static char ID; 30 AddMetadataPass(std::string PalMDString) 31 : ModulePass(ID), PalMDString(PalMDString) {} 32 bool runOnModule(Module &M) override { 33 auto &Ctx = M.getContext(); 34 auto *MD = M.getOrInsertNamedMetadata("amdgpu.pal.metadata.msgpack"); 35 auto *PalMD = MDString::get(Ctx, PalMDString); 36 auto *TMD = MDTuple::get(Ctx, {PalMD}); 37 MD->addOperand(TMD); 38 return true; 39 } 40 }; 41 char AddMetadataPass::ID = 0; 42 } // end anonymous namespace 43 44 class AMDGPUSelectionDAGTest : public testing::Test { 45 protected: 46 static void SetUpTestCase() { 47 InitializeAllTargets(); 48 InitializeAllTargetMCs(); 49 } 50 51 void SetUp() override { 52 std::string Error; 53 const Target *T = TargetRegistry::lookupTarget("amdgcn--amdpal", Error); 54 if (!T) 55 GTEST_SKIP(); 56 57 TargetOptions Options; 58 TM = std::unique_ptr<LLVMTargetMachine>( 59 static_cast<LLVMTargetMachine *>(T->createTargetMachine( 60 "amdgcn--amdpal", "gfx1010", "", Options, None))); 61 if (!TM) 62 GTEST_SKIP(); 63 64 LLVMContext Context; 65 std::unique_ptr<Module> M(new Module("TestModule", Context)); 66 M->setDataLayout(TM->createDataLayout()); 67 68 legacy::PassManager PM; 69 PM.add(new AddMetadataPass(PalMDString)); 70 raw_svector_ostream OutStream(Elf); 71 if (TM->addPassesToEmitFile(PM, OutStream, nullptr, 72 CodeGenFileType::CGFT_ObjectFile)) 73 report_fatal_error("Target machine cannot emit a file of this type"); 74 75 PM.run(*M); 76 } 77 78 static std::string PalMDString; 79 80 LLVMContext Context; 81 std::unique_ptr<LLVMTargetMachine> TM; 82 std::unique_ptr<Module> M; 83 SmallString<1024> Elf; 84 }; 85 std::string AMDGPUSelectionDAGTest::PalMDString = 86 "\x81\xB0" 87 "amdpal.pipelines\x91\x81\xA4.api\xA6Vulkan"; 88 89 TEST_F(AMDGPUSelectionDAGTest, checkMetadata) { 90 // Check that the string is contained in the ELF 91 EXPECT_NE(Elf.find("Vulkan"), std::string::npos); 92 } 93 94 } // end namespace llvm 95