1 //===- llvm/unittests/Target/AMDGPU/ExecMayBeModifiedBeforeAnyUse.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 #include "AMDGPUTargetMachine.h" 10 #include "GCNSubtarget.h" 11 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 12 #include "llvm/CodeGen/MachineModuleInfo.h" 13 #include "llvm/CodeGen/TargetSubtargetInfo.h" 14 #include "llvm/MC/MCTargetOptions.h" 15 #include "llvm/Support/TargetRegistry.h" 16 #include "llvm/Support/TargetSelect.h" 17 #include "llvm/Target/TargetMachine.h" 18 #include "gtest/gtest.h" 19 #include <thread> 20 21 using namespace llvm; 22 23 // implementation is in the llvm/unittests/Target/AMDGPU/DwarfRegMappings.cpp 24 std::unique_ptr<const GCNTargetMachine> 25 createTargetMachine(std::string TStr, StringRef CPU, StringRef FS); 26 27 TEST(AMDGPUExecMayBeModifiedBeforeAnyUse, TheTest) { 28 auto TM = createTargetMachine("amdgcn-amd-", "gfx906", ""); 29 if (!TM) 30 return; 31 32 GCNSubtarget ST(TM->getTargetTriple(), std::string(TM->getTargetCPU()), 33 std::string(TM->getTargetFeatureString()), *TM); 34 35 LLVMContext Ctx; 36 Module Mod("Module", Ctx); 37 Mod.setDataLayout(TM->createDataLayout()); 38 39 auto *Type = FunctionType::get(Type::getVoidTy(Ctx), false); 40 auto *F = Function::Create(Type, GlobalValue::ExternalLinkage, "Test", &Mod); 41 42 MachineModuleInfo MMI(TM.get()); 43 auto MF = std::make_unique<MachineFunction>(*F, *TM, ST, 42, MMI); 44 auto *BB = MF->CreateMachineBasicBlock(); 45 MF->push_back(BB); 46 47 auto E = BB->end(); 48 DebugLoc DL; 49 const auto &TII = *ST.getInstrInfo(); 50 auto &MRI = MF->getRegInfo(); 51 52 // create machine IR 53 Register R = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 54 55 MachineInstr *DefMI = 56 BuildMI(*BB, E, DL, TII.get(AMDGPU::S_MOV_B32), R).addImm(42).getInstr(); 57 58 auto First = 59 BuildMI(*BB, E, DL, TII.get(AMDGPU::S_NOP)).addReg(R, RegState::Implicit); 60 61 BuildMI(*BB, E, DL, TII.get(AMDGPU::S_NOP)).addReg(R, RegState::Implicit); 62 63 // this violates the continuous sequence of R's uses for the first S_NOP 64 First.addReg(R, RegState::Implicit); 65 66 #ifdef DEBUG_THIS_TEST 67 MF->dump(); 68 MRI.dumpUses(R); 69 #endif 70 71 // make sure execMayBeModifiedBeforeAnyUse doesn't crash 72 ASSERT_FALSE(execMayBeModifiedBeforeAnyUse(MRI, R, *DefMI)); 73 } 74