23
23
#include " llvm/ADT/StringSet.h"
24
24
#include " llvm/ADT/StringSwitch.h"
25
25
#include " llvm/ADT/Triple.h"
26
+ #include " llvm/Bitcode/BitcodeReader.h"
27
+ #include " llvm/Bitcode/BitcodeWriter.h"
26
28
#include " llvm/IR/LLVMContext.h"
29
+ #include " llvm/IRReader/IRReader.h"
27
30
#include " llvm/Object/Archive.h"
28
31
#include " llvm/Object/ArchiveWriter.h"
29
32
#include " llvm/Object/Binary.h"
40
43
#include " llvm/Support/Program.h"
41
44
#include " llvm/Support/Signals.h"
42
45
#include " llvm/Support/StringSaver.h"
46
+ #include " llvm/Support/TargetSelect.h"
43
47
#include " llvm/Support/WithColor.h"
44
48
#include " llvm/Support/raw_ostream.h"
45
49
#include < algorithm>
@@ -146,6 +150,13 @@ static bool hasHostKind(StringRef Target) {
146
150
return OffloadKind == " host" ;
147
151
}
148
152
153
+ static Triple getTargetTriple (StringRef Target) {
154
+ StringRef OffloadKind;
155
+ StringRef TargetTriple;
156
+ getOffloadKindAndTriple (Target, OffloadKind, TargetTriple);
157
+ return Triple (TargetTriple);
158
+ }
159
+
149
160
// / Generic file handler interface.
150
161
class FileHandler {
151
162
public:
@@ -557,6 +568,7 @@ class ObjectFileHandler final : public FileHandler {
557
568
Expected<SmallVector<char , 0 >> makeTargetSymbolTable () {
558
569
SmallVector<char , 0 > SymbolsBuf;
559
570
raw_svector_ostream SymbolsOS (SymbolsBuf);
571
+ LLVMContext Context;
560
572
561
573
for (unsigned I = 0 ; I < NumberOfInputs; ++I) {
562
574
if (I == HostInputIndex)
@@ -569,9 +581,36 @@ class ObjectFileHandler final : public FileHandler {
569
581
if (!BufOrErr)
570
582
return createFileError (InputFileNames[I], BufOrErr.getError ());
571
583
572
- LLVMContext Context;
584
+ std::unique_ptr<MemoryBuffer> Buf = std::move (*BufOrErr);
585
+
586
+ // Workaround for the absence of assembly parser for spir target. If this
587
+ // input is a bitcode for spir target we need to remove module-level
588
+ // inline asm from it, if there is one, and recreate the buffer with new
589
+ // contents.
590
+ // TODO: remove this workaround once spir target gets asm parser.
591
+ if (isBitcode ((const unsigned char *)Buf->getBufferStart (),
592
+ (const unsigned char *)Buf->getBufferEnd ()))
593
+ if (getTargetTriple (TargetNames[I]).isSPIR ()) {
594
+ SMDiagnostic Err;
595
+ std::unique_ptr<Module> Mod = parseIR (*Buf, Err, Context);
596
+ if (!Mod)
597
+ return createStringError (inconvertibleErrorCode (),
598
+ Err.getMessage ());
599
+
600
+ if (!Mod->getModuleInlineAsm ().empty ()) {
601
+ Mod->setModuleInlineAsm (" " );
602
+
603
+ SmallVector<char , 0 > ModuleBuf;
604
+ raw_svector_ostream ModuleOS (ModuleBuf);
605
+ WriteBitcodeToFile (*Mod, ModuleOS);
606
+
607
+ Buf = MemoryBuffer::getMemBufferCopy (ModuleOS.str (),
608
+ Buf->getBufferIdentifier ());
609
+ }
610
+ }
611
+
573
612
Expected<std::unique_ptr<Binary>> BinOrErr =
574
- createBinary (BufOrErr. get () ->getMemBufferRef (), &Context);
613
+ createBinary (Buf ->getMemBufferRef (), &Context);
575
614
576
615
// If it is not a symbolic file just ignore it since we cannot do anything
577
616
// with it.
@@ -1213,11 +1252,9 @@ class ArchiveFileHandler final : public FileHandler {
1213
1252
1214
1253
if (Mode == OutputType::Archive) {
1215
1254
// Determine archive kind for the offload target.
1216
- StringRef TargetKind;
1217
- StringRef TargetTriple;
1218
- getOffloadKindAndTriple (CurrBundle->first (), TargetKind, TargetTriple);
1219
- auto ArKind = Triple (TargetTriple).isOSDarwin () ? Archive::K_DARWIN
1220
- : Archive::K_GNU;
1255
+ auto ArKind = getTargetTriple (CurrBundle->first ()).isOSDarwin ()
1256
+ ? Archive::K_DARWIN
1257
+ : Archive::K_GNU;
1221
1258
1222
1259
// And write archive to the output.
1223
1260
Expected<std::unique_ptr<MemoryBuffer>> NewAr =
@@ -1527,6 +1564,11 @@ int main(int argc, const char **argv) {
1527
1564
return 0 ;
1528
1565
}
1529
1566
1567
+ // These calls are needed so that we can read bitcode correctly.
1568
+ InitializeAllTargetInfos ();
1569
+ InitializeAllTargetMCs ();
1570
+ InitializeAllAsmParsers ();
1571
+
1530
1572
auto reportError = [argv](Error E) {
1531
1573
logAllUnhandledErrors (std::move (E), WithColor::error (errs (), argv[0 ]));
1532
1574
};
0 commit comments