97 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9/// \file10/// Test that amdgpu metadata that is added in a pass is read by the asm emitter11/// and stored in the ELF.12//13//===----------------------------------------------------------------------===//14 15#include "llvm/IR/LegacyPassManager.h"16#include "llvm/IR/Module.h"17#include "llvm/MC/TargetRegistry.h"18#include "llvm/Pass.h"19#include "llvm/Support/TargetSelect.h"20#include "llvm/Target/TargetMachine.h"21#include "gtest/gtest.h"22 23namespace llvm {24 25namespace {26// Pass that adds global metadata27struct AddMetadataPass : public ModulePass {28 std::string PalMDString;29 30public:31 static char ID;32 AddMetadataPass(std::string PalMDString)33 : ModulePass(ID), PalMDString(PalMDString) {}34 bool runOnModule(Module &M) override {35 auto &Ctx = M.getContext();36 auto *MD = M.getOrInsertNamedMetadata("amdgpu.pal.metadata.msgpack");37 auto *PalMD = MDString::get(Ctx, PalMDString);38 auto *TMD = MDTuple::get(Ctx, {PalMD});39 MD->addOperand(TMD);40 return true;41 }42};43char AddMetadataPass::ID = 0;44} // end anonymous namespace45 46class AMDGPUSelectionDAGTest : public testing::Test {47protected:48 static void SetUpTestCase() {49 InitializeAllTargets();50 InitializeAllTargetMCs();51 }52 53 void SetUp() override {54 std::string Error;55 Triple TargetTriple("amdgcn--amdpal");56 const Target *T = TargetRegistry::lookupTarget(TargetTriple, Error);57 if (!T)58 GTEST_SKIP();59 60 TargetOptions Options;61 TM = std::unique_ptr<TargetMachine>(T->createTargetMachine(62 TargetTriple, "gfx1010", "", Options, std::nullopt));63 if (!TM)64 GTEST_SKIP();65 66 LLVMContext Context;67 std::unique_ptr<Module> M(new Module("TestModule", Context));68 M->setDataLayout(TM->createDataLayout());69 70 legacy::PassManager PM;71 PM.add(new AddMetadataPass(PalMDString));72 raw_svector_ostream OutStream(Elf);73 if (TM->addPassesToEmitFile(PM, OutStream, nullptr,74 CodeGenFileType::ObjectFile))75 report_fatal_error("Target machine cannot emit a file of this type");76 77 PM.run(*M);78 }79 80 static std::string PalMDString;81 82 LLVMContext Context;83 std::unique_ptr<TargetMachine> TM;84 std::unique_ptr<Module> M;85 SmallString<1024> Elf;86};87std::string AMDGPUSelectionDAGTest::PalMDString =88 "\x81\xB0"89 "amdpal.pipelines\x91\x81\xA4.api\xA6Vulkan";90 91TEST_F(AMDGPUSelectionDAGTest, checkMetadata) {92 // Check that the string is contained in the ELF93 EXPECT_NE(Elf.find("Vulkan"), std::string::npos);94}95 96} // end namespace llvm97