Skip to content

Commit 1580877

Browse files
authored
[Libomptarget] Remove bitcode image map used for JIT processing (#75672)
Summary: Libomptarget supports JIT by treating an LLVM-IR file as a regular input image. The handling here used a global map to keep track of triples once it was parsed. This was done to same time, however this created a global constructor as well as an extra mutex to handle it. This patch removes the use of this map. Instead, we simply use the file magic to perform a quick check if the input image is valid bitcode. If not, we then create a lazy module. This should roughly equivalent to the old handling that create an IR symbol table. Here we can prevent the module from materializing everything but the single triple metadata we read in later.
1 parent 2776795 commit 1580877

File tree

1 file changed

+19
-32
lines changed
  • openmp/libomptarget/plugins-nextgen/common/src

1 file changed

+19
-32
lines changed

openmp/libomptarget/plugins-nextgen/common/src/JIT.cpp

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@ using namespace llvm::object;
4747
using namespace omp;
4848
using namespace omp::target;
4949

50-
static codegen::RegisterCodeGenFlags RCGF;
51-
5250
namespace {
5351

54-
/// A map from a bitcode image start address to its corresponding triple. If the
55-
/// image is not in the map, it is not a bitcode image.
56-
DenseMap<void *, Triple::ArchType> BitcodeImageMap;
57-
std::shared_mutex BitcodeImageMapMutex;
52+
bool isImageBitcode(const __tgt_device_image &Image) {
53+
StringRef Binary(reinterpret_cast<const char *>(Image.ImageStart),
54+
target::getPtrDiff(Image.ImageEnd, Image.ImageStart));
55+
56+
return identify_magic(Binary) == file_magic::bitcode;
57+
}
5858

5959
std::once_flag InitFlag;
6060

6161
void init(Triple TT) {
62+
codegen::RegisterCodeGenFlags();
6263
#ifdef LIBOMPTARGET_JIT_NVPTX
6364
if (TT.isNVPTX()) {
6465
LLVMInitializeNVPTXTargetInfo();
@@ -323,44 +324,30 @@ JITEngine::process(const __tgt_device_image &Image,
323324
return Device.doJITPostProcessing(std::move(MB));
324325
};
325326

326-
{
327-
std::shared_lock<std::shared_mutex> SharedLock(BitcodeImageMapMutex);
328-
auto Itr = BitcodeImageMap.find(Image.ImageStart);
329-
if (Itr != BitcodeImageMap.end() && Itr->second == TT.getArch())
330-
return compile(Image, ComputeUnitKind, PostProcessing);
331-
}
327+
if (isImageBitcode(Image))
328+
return compile(Image, ComputeUnitKind, PostProcessing);
332329

333330
return &Image;
334331
}
335332

336333
bool JITEngine::checkBitcodeImage(const __tgt_device_image &Image) {
337334
TimeTraceScope TimeScope("Check bitcode image");
338-
std::lock_guard<std::shared_mutex> Lock(BitcodeImageMapMutex);
339335

340-
{
341-
auto Itr = BitcodeImageMap.find(Image.ImageStart);
342-
if (Itr != BitcodeImageMap.end() && Itr->second == TT.getArch())
343-
return true;
344-
}
336+
if (!isImageBitcode(Image))
337+
return false;
345338

346339
StringRef Data(reinterpret_cast<const char *>(Image.ImageStart),
347340
target::getPtrDiff(Image.ImageEnd, Image.ImageStart));
348-
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(
349-
Data, /* BufferName */ "", /* RequiresNullTerminator */ false);
341+
auto MB = MemoryBuffer::getMemBuffer(Data, /*BufferName=*/"",
342+
/*RequiresNullTerminator=*/false);
350343
if (!MB)
351344
return false;
352345

353-
Expected<object::IRSymtabFile> FOrErr = object::readIRSymtab(*MB);
354-
if (!FOrErr) {
355-
consumeError(FOrErr.takeError());
356-
return false;
357-
}
358-
359-
auto ActualTriple = FOrErr->TheReader.getTargetTriple();
360-
auto BitcodeTA = Triple(ActualTriple).getArch();
361-
BitcodeImageMap[Image.ImageStart] = BitcodeTA;
362-
363-
DP("Is%s IR Image\n", BitcodeTA == TT.getArch() ? " " : " NOT");
346+
LLVMContext Context;
347+
SMDiagnostic Diagnostic;
348+
std::unique_ptr<Module> M =
349+
llvm::getLazyIRModule(std::move(MB), Diagnostic, Context,
350+
/*ShouldLazyLoadMetadata=*/true);
364351

365-
return BitcodeTA == TT.getArch();
352+
return M && Triple(M->getTargetTriple()).getArch() == TT.getArch();
366353
}

0 commit comments

Comments
 (0)