forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcomgr-compiler.cpp
1728 lines (1458 loc) · 52.9 KB
/
comgr-compiler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* University of Illinois/NCSA
* Open Source License
*
* Copyright (c) 2003-2017 University of Illinois at Urbana-Champaign.
* Modifications (c) 2018 Advanced Micro Devices, Inc.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* with the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the names of the LLVM Team, University of Illinois at
* Urbana-Champaign, nor the names of its contributors may be used to
* endorse or promote products derived from this Software without specific
* prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
* THE SOFTWARE.
*
******************************************************************************/
#include "comgr-compiler.h"
#include "comgr-device-libs.h"
#include "comgr-env.h"
#include "lld/Common/CommonLinkerContext.h"
#include "lld/Common/Driver.h"
#include "clang/Basic/Version.h"
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Job.h"
#include "clang/Driver/OffloadBundler.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/Tool.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/FrontendTool/Utils.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Linker/Linker.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/Archive.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/WithColor.h"
#include "llvm/TargetParser/Host.h"
#include "time-stat/ts-interface.h"
#include <csignal>
LLD_HAS_DRIVER(elf)
using namespace llvm;
using namespace llvm::opt;
using namespace llvm::sys;
using namespace clang;
using namespace clang::driver;
using namespace clang::driver::options;
using namespace TimeStatistics;
namespace COMGR {
namespace {
static constexpr llvm::StringLiteral LinkerJobName = "amdgpu::Linker";
/// \brief Helper class for representing a single invocation of the assembler.
struct AssemblerInvocation {
/// @name Target Options
/// @{
/// The name of the target triple to assemble for.
std::string Triple;
/// If given, the name of the target CPU to determine which instructions
/// are legal.
std::string CPU;
/// The list of target specific features to enable or disable -- this should
/// be a list of strings starting with '+' or '-'.
std::vector<std::string> Features;
/// The list of symbol definitions.
std::vector<std::string> SymbolDefs;
/// @}
/// @name Language Options
/// @{
std::vector<std::string> IncludePaths;
unsigned NoInitialTextSection : 1;
unsigned SaveTemporaryLabels : 1;
unsigned GenDwarfForAssembly : 1;
unsigned RelaxELFRelocations : 1;
unsigned DwarfVersion;
std::string DwarfDebugFlags;
std::string DwarfDebugProducer;
std::string DebugCompilationDir;
llvm::DebugCompressionType CompressDebugSections =
llvm::DebugCompressionType::None;
std::string MainFileName;
/// @}
/// @name Frontend Options
/// @{
std::string InputFile;
std::vector<std::string> LLVMArgs;
std::string OutputPath;
enum FileType {
FT_Asm, ///< Assembly (.s) output, transliterate mode.
FT_Null, ///< No output, for timing purposes.
FT_Obj ///< Object file output.
};
FileType OutputType;
unsigned ShowHelp : 1;
unsigned ShowVersion : 1;
/// @}
/// @name Transliterate Options
/// @{
unsigned OutputAsmVariant;
unsigned ShowEncoding : 1;
unsigned ShowInst : 1;
/// @}
/// @name Assembler Options
/// @{
unsigned RelaxAll : 1;
unsigned NoExecStack : 1;
unsigned FatalWarnings : 1;
unsigned IncrementalLinkerCompatible : 1;
/// The name of the relocation model to use.
std::string RelocationModel;
/// @}
public:
AssemblerInvocation() {
Triple = "";
NoInitialTextSection = 0;
InputFile = "-";
OutputPath = "-";
OutputType = FT_Asm;
OutputAsmVariant = 0;
ShowInst = 0;
ShowEncoding = 0;
RelaxAll = 0;
NoExecStack = 0;
FatalWarnings = 0;
IncrementalLinkerCompatible = 0;
DwarfVersion = 0;
}
static bool createFromArgs(AssemblerInvocation &Res,
ArrayRef<const char *> Argv,
DiagnosticsEngine &Diags);
};
} // namespace
bool AssemblerInvocation::createFromArgs(AssemblerInvocation &Opts,
ArrayRef<const char *> Argv,
DiagnosticsEngine &Diags) {
bool Success = true;
// Parse the arguments.
const OptTable &OptTbl = getDriverOptTable();
const unsigned IncludedFlagsBitmask = options::CC1AsOption;
unsigned MissingArgIndex, MissingArgCount;
InputArgList Args = OptTbl.ParseArgs(Argv, MissingArgIndex, MissingArgCount,
IncludedFlagsBitmask);
// Check for missing argument error.
if (MissingArgCount) {
Diags.Report(diag::err_drv_missing_argument)
<< Args.getArgString(MissingArgIndex) << MissingArgCount;
Success = false;
}
// Issue errors on unknown arguments.
for (const Arg *A : Args.filtered(OPT_UNKNOWN)) {
auto ArgString = A->getAsString(Args);
std::string Nearest;
if (OptTbl.findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1) {
Diags.Report(diag::err_drv_unknown_argument) << ArgString;
} else {
Diags.Report(diag::err_drv_unknown_argument_with_suggestion)
<< ArgString << Nearest;
}
Success = false;
}
// Construct the invocation.
// Target Options
Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu));
Opts.Features = Args.getAllArgValues(OPT_target_feature);
// Use the default target triple if unspecified.
if (Opts.Triple.empty()) {
Opts.Triple = llvm::sys::getDefaultTargetTriple();
}
// Language Options
Opts.IncludePaths = Args.getAllArgValues(OPT_I);
Opts.NoInitialTextSection = Args.hasArg(OPT_n);
Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels);
// Any DebugInfoKind implies GenDwarfForAssembly.
Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ);
if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections,
OPT_compress_debug_sections_EQ)) {
if (A->getOption().getID() == OPT_compress_debug_sections) {
// TODO: be more clever about the compression type auto-detection
Opts.CompressDebugSections = llvm::DebugCompressionType::Zlib;
} else {
Opts.CompressDebugSections =
llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue())
.Case("none", llvm::DebugCompressionType::None)
.Case("zlib", llvm::DebugCompressionType::Zlib)
.Default(llvm::DebugCompressionType::None);
}
}
Opts.RelaxELFRelocations = !Args.hasArg(OPT_mrelax_relocations_no);
Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags);
Opts.DwarfDebugFlags =
std::string(Args.getLastArgValue(OPT_dwarf_debug_flags));
Opts.DwarfDebugProducer =
std::string(Args.getLastArgValue(OPT_dwarf_debug_producer));
Opts.DebugCompilationDir =
std::string(Args.getLastArgValue(OPT_fdebug_compilation_dir));
Opts.MainFileName = std::string(Args.getLastArgValue(OPT_main_file_name));
// Frontend Options
if (Args.hasArg(OPT_INPUT)) {
bool First = true;
for (const Arg *A : Args.filtered(OPT_INPUT)) {
if (First) {
Opts.InputFile = A->getValue();
First = false;
} else {
Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args);
Success = false;
}
}
}
Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
Opts.OutputPath = std::string(Args.getLastArgValue(OPT_o));
if (Arg *A = Args.getLastArg(OPT_filetype)) {
StringRef Name = A->getValue();
unsigned OutputType = StringSwitch<unsigned>(Name)
.Case("asm", FT_Asm)
.Case("null", FT_Null)
.Case("obj", FT_Obj)
.Default(~0U);
if (OutputType == ~0U) {
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
Success = false;
} else {
Opts.OutputType = FileType(OutputType);
}
}
Opts.ShowHelp = Args.hasArg(OPT_help);
Opts.ShowVersion = Args.hasArg(OPT_version);
// Transliterate Options
Opts.OutputAsmVariant =
getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags);
Opts.ShowEncoding = Args.hasArg(OPT_show_encoding);
Opts.ShowInst = Args.hasArg(OPT_show_inst);
// Assemble Options
Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
Opts.RelocationModel =
std::string(Args.getLastArgValue(OPT_mrelocation_model, "pic"));
Opts.IncrementalLinkerCompatible =
Args.hasArg(OPT_mincremental_linker_compatible);
Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
return Success;
}
static std::unique_ptr<raw_fd_ostream>
getOutputStream(AssemblerInvocation &Opts, DiagnosticsEngine &Diags,
bool Binary) {
if (Opts.OutputPath.empty()) {
Opts.OutputPath = "-";
}
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT.
if (Opts.OutputPath != "-") {
sys::RemoveFileOnSignal(Opts.OutputPath);
}
std::error_code EC;
auto Out = std::make_unique<raw_fd_ostream>(
Opts.OutputPath, EC, (Binary ? sys::fs::OF_None : sys::fs::OF_Text));
if (EC) {
Diags.Report(diag::err_fe_unable_to_open_output)
<< Opts.OutputPath << EC.message();
return nullptr;
}
return Out;
}
static bool executeAssemblerImpl(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags, raw_ostream &LogS) {
// Get the target specific parser.
std::string Error;
const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
if (!TheTarget) {
return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
}
ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
if (std::error_code EC = Buffer.getError()) {
Error = EC.message();
return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
}
SourceMgr SrcMgr;
SrcMgr.setDiagHandler(
[](const SMDiagnostic &SMDiag, void *LogS) {
SMDiag.print("", *(raw_ostream *)LogS, /* ShowColors */ false);
},
&LogS);
// Tell SrcMgr about this buffer, which is what the parser will pick up.
SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
// Record the location of the include directories so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(Opts.IncludePaths);
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
assert(MRI && "Unable to create target register info!");
llvm::MCTargetOptions MCOptions;
std::unique_ptr<MCAsmInfo> MAI(
TheTarget->createMCAsmInfo(*MRI, Opts.Triple, MCOptions));
assert(MAI && "Unable to create target asm info!");
// Ensure MCAsmInfo initialization occurs before any use, otherwise sections
// may be created with a combination of default and explicit settings.
MAI->setCompressDebugSections(Opts.CompressDebugSections);
MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations);
bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
std::unique_ptr<raw_fd_ostream> FDOS = getOutputStream(Opts, Diags, IsBinary);
if (!FDOS) {
return true;
}
// Build up the feature string from the target feature list.
std::string FS;
if (!Opts.Features.empty()) {
FS = Opts.Features[0];
for (unsigned I = 1, E = Opts.Features.size(); I != E; ++I) {
FS += "," + Opts.Features[I];
}
}
std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
std::unique_ptr<MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
MCContext Ctx(Triple(Opts.Triple), MAI.get(), MRI.get(),
STI.get(), &SrcMgr);
Ctx.setObjectFileInfo(MOFI.get());
bool PIC = false;
if (Opts.RelocationModel == "static") {
PIC = false;
} else if (Opts.RelocationModel == "pic") {
PIC = true;
} else {
assert(Opts.RelocationModel == "dynamic-no-pic" && "Invalid PIC model!");
PIC = false;
}
MOFI->initMCObjectFileInfo(Ctx, PIC);
if (Opts.SaveTemporaryLabels) {
Ctx.setAllowTemporaryLabels(false);
}
if (Opts.GenDwarfForAssembly) {
Ctx.setGenDwarfForAssembly(true);
}
if (!Opts.DwarfDebugFlags.empty()) {
Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
}
if (!Opts.DwarfDebugProducer.empty()) {
Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
}
if (!Opts.DebugCompilationDir.empty()) {
Ctx.setCompilationDir(Opts.DebugCompilationDir);
}
if (!Opts.MainFileName.empty()) {
Ctx.setMainFileName(StringRef(Opts.MainFileName));
}
Ctx.setDwarfVersion(Opts.DwarfVersion);
std::unique_ptr<MCStreamer> Str;
std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
raw_pwrite_stream *Out = FDOS.get();
std::unique_ptr<buffer_ostream> BOS;
// FIXME: There is a bit of code duplication with addPassesToEmitFile.
if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
MCInstPrinter *IP = TheTarget->createMCInstPrinter(
llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
std::unique_ptr<MCCodeEmitter> MCE;
std::unique_ptr<MCAsmBackend> MAB;
if (Opts.ShowEncoding) {
MCE.reset(TheTarget->createMCCodeEmitter(*MCII, Ctx));
MCTargetOptions Options;
MAB.reset(TheTarget->createMCAsmBackend(*STI, *MRI, Options));
}
auto FOut = std::make_unique<formatted_raw_ostream>(*Out);
Str.reset(TheTarget->createAsmStreamer(
Ctx, std::move(FOut), /*asmverbose*/ true,
/*useDwarfDirectory*/ true, IP, std::move(MCE), std::move(MAB),
Opts.ShowInst));
} else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
Str.reset(createNullStreamer(Ctx));
} else {
assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&
"Invalid file type!");
if (!FDOS->supportsSeeking()) {
BOS = std::make_unique<buffer_ostream>(*FDOS);
Out = BOS.get();
}
MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, Ctx);
MCTargetOptions Options;
MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, Options);
Triple T(Opts.Triple);
Str.reset(TheTarget->createMCObjectStreamer(
T, Ctx, std::unique_ptr<MCAsmBackend>(MAB),
MAB->createObjectWriter(*Out), std::unique_ptr<MCCodeEmitter>(CE), *STI,
Opts.RelaxAll, Opts.IncrementalLinkerCompatible,
/*DWARFMustBeAtTheEnd*/ true));
Str.get()->initSections(Opts.NoExecStack, *STI);
}
bool Failed = false;
std::unique_ptr<MCAsmParser> Parser(
createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
// FIXME: init MCTargetOptions from sanitizer flags here.
MCTargetOptions Options;
std::unique_ptr<MCTargetAsmParser> TAP(
TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
if (!TAP) {
Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
}
// Set values for symbols, if any.
for (auto &S : Opts.SymbolDefs) {
auto Pair = StringRef(S).split('=');
auto Sym = Pair.first;
auto Val = Pair.second;
int64_t Value;
// We have already error checked this in the driver.
if (!Val.getAsInteger(0, Value)) {
Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value);
}
}
if (!Failed) {
Parser->setTargetParser(*TAP.get());
Failed = Parser->Run(Opts.NoInitialTextSection);
}
return Failed;
}
static bool executeAssembler(AssemblerInvocation &Opts,
DiagnosticsEngine &Diags, raw_ostream &LogS) {
bool Failed = executeAssemblerImpl(Opts, Diags, LogS);
// Delete output file if there were errors.
if (Failed && Opts.OutputPath != "-") {
sys::fs::remove(Opts.OutputPath);
}
return Failed;
}
static SmallString<128> getFilePath(DataObject *Object, StringRef Dir) {
SmallString<128> Path(Dir);
path::append(Path, Object->Name);
// Create directories specified in the File Path so that the in-process driver
// can successfully execute clang commands that use this file path as an
// output argument
if (fs::create_directories(path::parent_path(Path))) {
return SmallString<128>();
}
return Path;
}
static amd_comgr_status_t inputFromFile(DataObject *Object, StringRef Path) {
ProfilePoint Point("FileIO");
auto BufOrError = MemoryBuffer::getFile(Path);
if (std::error_code EC = BufOrError.getError()) {
return AMD_COMGR_STATUS_ERROR;
}
Object->setData(BufOrError.get()->getBuffer());
return AMD_COMGR_STATUS_SUCCESS;
}
static amd_comgr_status_t outputToFile(StringRef Data, StringRef Path) {
SmallString<128> DirPath = Path;
path::remove_filename(DirPath);
{
ProfilePoint Point("CreateDir");
if (fs::create_directories(DirPath)) {
return AMD_COMGR_STATUS_ERROR;
}
}
std::error_code EC;
ProfilePoint Point("FileIO");
raw_fd_ostream OS(Path, EC, fs::OF_None);
if (EC) {
return AMD_COMGR_STATUS_ERROR;
}
OS << Data;
OS.close();
if (OS.has_error()) {
return AMD_COMGR_STATUS_ERROR;
}
return AMD_COMGR_STATUS_SUCCESS;
}
static amd_comgr_status_t outputToFile(DataObject *Object, StringRef Path) {
return outputToFile(StringRef(Object->Data, Object->Size), Path);
}
static void initializeCommandLineArgs(SmallVectorImpl<const char *> &Args) {
// Workaround for flawed Driver::BuildCompilation(...) implementation,
// which eliminates 1st argument, cause it actually awaits argv[0].
Args.clear();
Args.push_back("");
}
// Parse -mllvm options
static amd_comgr_status_t
parseLLVMOptions(const std::vector<std::string> &Options) {
std::vector<const char *> LLVMArgs;
for (auto Option : Options) {
LLVMArgs.push_back("");
LLVMArgs.push_back(Option.c_str());
if (!cl::ParseCommandLineOptions(LLVMArgs.size(), &LLVMArgs[0],
"-mllvm options parsing")) {
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
}
LLVMArgs.clear();
}
return AMD_COMGR_STATUS_SUCCESS;
}
static amd_comgr_status_t linkWithLLD(llvm::ArrayRef<const char *> Args,
llvm::raw_ostream &LogS,
llvm::raw_ostream &LogE) {
ArgStringList LLDArgs(llvm::iterator_range<ArrayRef<const char *>::iterator>(
Args.begin(), Args.end()));
LLDArgs.insert(LLDArgs.begin(), "ld.lld");
LLDArgs.push_back("--threads=1");
ArrayRef<const char *> ArgRefs = llvm::ArrayRef(LLDArgs);
lld::Result LLDRet =
lld::lldMain(ArgRefs, LogS, LogE, {{lld::Gnu, &lld::elf::link}});
lld::CommonLinkerContext::destroy();
if (LLDRet.retCode || !LLDRet.canRunAgain) {
return AMD_COMGR_STATUS_ERROR;
}
return AMD_COMGR_STATUS_SUCCESS;
}
static void logArgv(raw_ostream &OS, StringRef ProgramName,
ArrayRef<const char *> Argv) {
OS << " Driver Job Args: " << ProgramName;
for (size_t I = 0; I < Argv.size(); ++I) {
// Skip the first argument, which we replace with ProgramName, and the last
// argument, which is a null terminator.
if (I && Argv[I]) {
OS << " \"" << Argv[I] << '\"';
}
}
OS << '\n';
OS.flush();
}
amd_comgr_status_t
AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
// A DiagnosticsEngine is required at several points:
// * By the Driver in order to diagnose option parsing.
// * By the CompilerInvocation in order to diagnose option parsing.
// * By the CompilerInstance in order to diagnose everything else.
// It is a chicken-and-egg problem in that you need some form of diagnostics
// in order to diagnose options which further influence diagnostics. The code
// here is mostly copy-and-pasted from driver.cpp/cc1_main.cpp/various Clang
// tests to try to approximate the same behavior as running the `clang`
// executable.
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
unsigned MissingArgIndex, MissingArgCount;
InputArgList ArgList = getDriverOptTable().ParseArgs(
Args.slice(1), MissingArgIndex, MissingArgCount);
// We ignore MissingArgCount and the return value of ParseDiagnosticArgs. Any
// errors that would be diagnosed here will also be diagnosed later, when the
// DiagnosticsEngine actually exists.
(void)ParseDiagnosticArgs(*DiagOpts, ArgList);
TextDiagnosticPrinter *DiagClient =
new TextDiagnosticPrinter(LogS, &*DiagOpts);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs);
DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
Driver TheDriver((Twine(env::getLLVMPath()) + "/bin/clang").str(), "", Diags);
TheDriver.setTitle("AMDGPU Code Object Manager");
TheDriver.setCheckInputsExist(false);
// Log arguments used to build compilation
if (env::shouldEmitVerboseLogs()) {
LogS << " Compilation Args: " ;
for (size_t i = 1; i < Args.size(); ++i) {
if (Args[i]) {
LogS << " \"" << Args[i] << '\"';
}
}
LogS << '\n';
LogS.flush();
}
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
if (!C) {
return C->containsError() ? AMD_COMGR_STATUS_ERROR
: AMD_COMGR_STATUS_SUCCESS;
}
for (auto &Job : C->getJobs()) {
auto Arguments = Job.getArguments();
SmallVector<const char *, 128> Argv;
initializeCommandLineArgs(Argv);
Argv.append(Arguments.begin(), Arguments.end());
Argv.push_back(nullptr);
// By default clang driver will ask CC1 to leak memory.
auto *IT = find(Argv, StringRef("-disable-free"));
if (IT != Argv.end()) {
Argv.erase(IT);
}
clearLLVMOptions();
if (Argv[1] == StringRef("-cc1")) {
if (env::shouldEmitVerboseLogs()) {
logArgv(LogS, "clang", Argv);
}
std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
Clang->setVerboseOutputStream(LogS);
if (!Argv.back()) {
Argv.pop_back();
}
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
Diags)) {
return AMD_COMGR_STATUS_ERROR;
}
// Internally this call refers to the invocation created above, so at
// this point the DiagnosticsEngine should accurately reflect all user
// requested configuration from Argv.
Clang->createDiagnostics(DiagClient, /* ShouldOwnClient */ false);
if (!Clang->hasDiagnostics()) {
return AMD_COMGR_STATUS_ERROR;
}
if (!ExecuteCompilerInvocation(Clang.get())) {
return AMD_COMGR_STATUS_ERROR;
}
} else if (Argv[1] == StringRef("-cc1as")) {
if (env::shouldEmitVerboseLogs()) {
logArgv(LogS, "clang", Argv);
}
Argv.erase(Argv.begin() + 1);
if (!Argv.back()) {
Argv.pop_back();
}
AssemblerInvocation Asm;
if (!AssemblerInvocation::createFromArgs(Asm, Argv, Diags)) {
return AMD_COMGR_STATUS_ERROR;
}
if (auto Status = parseLLVMOptions(Asm.LLVMArgs)) {
return Status;
}
if (executeAssembler(Asm, Diags, LogS)) {
return AMD_COMGR_STATUS_ERROR;
}
} else if (Job.getCreator().getName() == LinkerJobName) {
if (env::shouldEmitVerboseLogs()) {
logArgv(LogS, "lld", Argv);
}
if (auto Status = linkWithLLD(Arguments, LogS, LogS)) {
return Status;
}
} else {
return AMD_COMGR_STATUS_ERROR;
}
}
return AMD_COMGR_STATUS_SUCCESS;
}
amd_comgr_status_t AMDGPUCompiler::createTmpDirs() {
ProfilePoint Point("CreateDir");
if (fs::createUniqueDirectory("comgr", TmpDir)) {
return AMD_COMGR_STATUS_ERROR;
}
InputDir = TmpDir;
path::append(InputDir, "input");
if (fs::create_directory(InputDir)) {
return AMD_COMGR_STATUS_ERROR;
}
OutputDir = TmpDir;
path::append(OutputDir, "output");
if (fs::create_directory(OutputDir)) {
return AMD_COMGR_STATUS_ERROR;
}
IncludeDir = TmpDir;
path::append(IncludeDir, "include");
if (fs::create_directory(IncludeDir)) {
return AMD_COMGR_STATUS_ERROR;
}
return AMD_COMGR_STATUS_SUCCESS;
}
// On windows fs::remove_directories takes huge time so use fs::remove.
amd_comgr_status_t RemoveDirectory(const StringRef DirName) {
std::error_code EC;
for (fs::directory_iterator Dir(DirName, EC), DirEnd;
Dir != DirEnd && !EC; Dir.increment(EC)) {
const StringRef Path = Dir->path();
fs::file_status Status;
EC = fs::status(Path, Status);
if (EC) {
return AMD_COMGR_STATUS_ERROR;
}
switch (Status.type()) {
case fs::file_type::regular_file:
if (fs::remove(Path)) {
return AMD_COMGR_STATUS_ERROR;
}
break;
case fs::file_type::directory_file:
if (RemoveDirectory(Path)) {
return AMD_COMGR_STATUS_ERROR;
}
if (fs::remove(Path)) {
return AMD_COMGR_STATUS_ERROR;
}
break;
default:
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
}
}
if (fs::remove(DirName)) {
return AMD_COMGR_STATUS_ERROR;
}
return AMD_COMGR_STATUS_SUCCESS;
}
amd_comgr_status_t AMDGPUCompiler::removeTmpDirs() {
if (TmpDir.empty()) {
return AMD_COMGR_STATUS_SUCCESS;
}
ProfilePoint Point("RemoveDir");
#ifndef _WIN32
if (fs::remove_directories(TmpDir)) {
return AMD_COMGR_STATUS_ERROR;
}
return AMD_COMGR_STATUS_SUCCESS;
#else
return RemoveDirectory(TmpDir);
#endif
}
amd_comgr_status_t AMDGPUCompiler::executeOutOfProcessHIPCompilation(
llvm::ArrayRef<const char *> Args) {
std::string Exec = (Twine(env::getHIPPath()) + "/bin/hipcc").str();
std::vector<StringRef> ArgsV;
ArgsV.push_back(Exec);
for (unsigned I = 0, E = Args.size(); I != E; ++I) {
if (strcmp(Args[I], "-hip-path") == 0) {
++I;
if (I == E) {
LogS << "Error: -hip-path option misses argument.\n";
return AMD_COMGR_STATUS_ERROR;
}
Exec = (Twine(Args[I]) + "/bin/hipcc").str();
ArgsV[0] = Exec;
} else {
ArgsV.push_back(Args[I]);
}
}
ArgsV.push_back("--genco");
if (env::shouldEmitVerboseLogs()) {
LogS << "\t hipcc Command: ";
for (auto A : ArgsV)
LogS << A << " ";
LogS << "\n";
}
llvm::ArrayRef<std::optional<StringRef>> Redirects;
std::string ErrMsg;
int RC = sys::ExecuteAndWait(Exec, ArgsV,
/*env=*/std::nullopt, Redirects, /*secondsToWait=*/0,
/*memoryLimit=*/0, &ErrMsg);
LogS << ErrMsg;
return RC ? AMD_COMGR_STATUS_ERROR : AMD_COMGR_STATUS_SUCCESS;
}
amd_comgr_status_t AMDGPUCompiler::processFile(const char *InputFilePath,
const char *OutputFilePath) {
SmallVector<const char *, 128> Argv;
for (auto &Arg : Args) {
Argv.push_back(Arg);
}
for (auto &Option : ActionInfo->getOptions()) {
Argv.push_back(Option.c_str());
if (Option.rfind("--rocm-path", 0) == 0) {
NoGpuLib = false;
}
}
// The ROCm device library should be provided via --rocm-path. Otherwise
// we can pass -nogpulib to build without the ROCm device library
if (NoGpuLib) {
Argv.push_back("-nogpulib");
}
if (getLanguage() == AMD_COMGR_LANGUAGE_HIP && env::shouldSaveTemps()) {
std::string save_tmps = "-save-temps=" + OutputDir.str().str();
Argv.push_back(strdup(save_tmps.c_str()));
}
Argv.push_back(InputFilePath);
Argv.push_back("-o");
Argv.push_back(OutputFilePath);
// For HIP OOP compilation, we launch a process.
if (CompileOOP && getLanguage() == AMD_COMGR_LANGUAGE_HIP) {
return executeOutOfProcessHIPCompilation(Argv);
}
return executeInProcessDriver(Argv);
}
amd_comgr_status_t
AMDGPUCompiler::processFiles(amd_comgr_data_kind_t OutputKind,
const char *OutputSuffix) {
for (auto *Input : InSet->DataObjects) {
if (Input->DataKind != AMD_COMGR_DATA_KIND_INCLUDE) {
continue;
}
auto IncludeFilePath = getFilePath(Input, IncludeDir);
if (auto Status = outputToFile(Input, IncludeFilePath)) {
return Status;
}
}
for (auto *Input : InSet->DataObjects) {
if (Input->DataKind != AMD_COMGR_DATA_KIND_SOURCE &&
Input->DataKind != AMD_COMGR_DATA_KIND_BC &&
Input->DataKind != AMD_COMGR_DATA_KIND_RELOCATABLE &&
Input->DataKind != AMD_COMGR_DATA_KIND_EXECUTABLE) {
continue;
}
auto InputFilePath = getFilePath(Input, InputDir);
if (auto Status = outputToFile(Input, InputFilePath)) {
return Status;
}
amd_comgr_data_t OutputT;
if (auto Status = amd_comgr_create_data(OutputKind, &OutputT)) {
return Status;
}
ScopedDataObjectReleaser SDOR(OutputT);
DataObject *Output = DataObject::convert(OutputT);
Output->setName(std::string(Input->Name) + OutputSuffix);
auto OutputFilePath = getFilePath(Output, OutputDir);
if (auto Status =
processFile(InputFilePath.c_str(), OutputFilePath.c_str())) {
return Status;
}
if (auto Status = inputFromFile(Output, OutputFilePath)) {
return Status;
}
if (auto Status = amd_comgr_data_set_add(OutSetT, OutputT)) {
return Status;
}
}
return AMD_COMGR_STATUS_SUCCESS;
}
amd_comgr_status_t AMDGPUCompiler::addIncludeFlags() {
if (ActionInfo->Path) {
Args.push_back("-I");
Args.push_back(ActionInfo->Path);
}
Args.push_back("-I");
Args.push_back(IncludeDir.c_str());
for (auto *Input : InSet->DataObjects) {
if (Input->DataKind != AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER) {
continue;
}
PrecompiledHeaders.push_back(getFilePath(Input, IncludeDir));
auto &PrecompiledHeaderPath = PrecompiledHeaders.back();
if (auto Status = outputToFile(Input, PrecompiledHeaderPath)) {
return Status;
}
Args.push_back("-include-pch");
Args.push_back(PrecompiledHeaderPath.c_str());
Args.push_back("-Xclang");
Args.push_back("-fno-validate-pch");
}
return AMD_COMGR_STATUS_SUCCESS;