Skip to content

Commit 7a1a3d6

Browse files
Add MCCAS options to swift and guard mccas.swift properly
Revert "Revert "Add driver options to swift to enable MCCAS."" This reverts commit 0e8554b.
1 parent 97717d7 commit 7a1a3d6

File tree

11 files changed

+122
-25
lines changed

11 files changed

+122
-25
lines changed

Diff for: include/swift/AST/IRGenOptions.h

+17-9
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,15 @@ class IRGenOptions {
491491
/// The calling convention used to perform non-swift calls.
492492
llvm::CallingConv::ID PlatformCCallingConvention;
493493

494+
/// Use CAS based object format as the output.
495+
bool UseCASBackend;
496+
497+
/// The output mode for the CAS Backend.
498+
llvm::CASBackendMode CASObjMode;
499+
500+
/// Emit a .casid file next to the object file if CAS Backend is used.
501+
bool EmitCASIDFile;
502+
494503
IRGenOptions()
495504
: DWARFVersion(2),
496505
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
@@ -502,9 +511,9 @@ class IRGenOptions {
502511
DebugInfoFormat(IRGenDebugInfoFormat::None),
503512
DisableClangModuleSkeletonCUs(false), UseJIT(false),
504513
DisableLLVMOptzns(false), DisableSwiftSpecificLLVMOptzns(false),
505-
Playground(false),
506-
EmitStackPromotionChecks(false), UseSingleModuleLLVMEmission(false),
507-
FunctionSections(false), PrintInlineTree(false), AlwaysCompile(false),
514+
Playground(false), EmitStackPromotionChecks(false),
515+
UseSingleModuleLLVMEmission(false), FunctionSections(false),
516+
PrintInlineTree(false), AlwaysCompile(false),
508517
EmbedMode(IRGenEmbedMode::None), LLVMLTOKind(IRGenLLVMLTOKind::None),
509518
SwiftAsyncFramePointer(SwiftAsyncFramePointerKind::Auto),
510519
HasValueNamesSetting(false), ValueNames(false),
@@ -526,13 +535,12 @@ class IRGenOptions {
526535
WitnessMethodElimination(false), ConditionalRuntimeRecords(false),
527536
InternalizeAtLink(false), InternalizeSymbols(false),
528537
EmitGenericRODatas(false), NoPreallocatedInstantiationCaches(false),
529-
DisableReadonlyStaticObjects(false),
530-
CollocatedMetadataFunctions(false),
531-
ColocateTypeDescriptors(true),
532-
UseRelativeProtocolWitnessTables(false), CmdArgs(),
533-
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
538+
DisableReadonlyStaticObjects(false), CollocatedMetadataFunctions(false),
539+
ColocateTypeDescriptors(true), UseRelativeProtocolWitnessTables(false),
540+
CmdArgs(), SanitizeCoverage(llvm::SanitizerCoverageOptions()),
534541
TypeInfoFilter(TypeInfoDumpFilter::All),
535-
PlatformCCallingConvention(llvm::CallingConv::C) {
542+
PlatformCCallingConvention(llvm::CallingConv::C), UseCASBackend(false),
543+
CASObjMode(llvm::CASBackendMode::Native) {
536544
#ifndef NDEBUG
537545
DisableRoundTripDebugTypes = false;
538546
#else

Diff for: include/swift/Frontend/Frontend.h

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ class CompilerInvocation {
164164
return LangOpts.Target.str();
165165
}
166166

167+
bool requiresCAS() const {
168+
return FrontendOpts.EnableCaching || FrontendOpts.UseCASBackend;
169+
}
170+
167171
void setClangModuleCachePath(StringRef Path) {
168172
ClangImporterOpts.ModuleCachePath = Path.str();
169173
}

Diff for: include/swift/Frontend/FrontendOptions.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
#define SWIFT_FRONTEND_FRONTENDOPTIONS_H
1515

1616
#include "swift/Basic/FileTypes.h"
17-
#include "swift/Basic/Version.h"
1817
#include "swift/Basic/PathRemapper.h"
18+
#include "swift/Basic/Version.h"
1919
#include "swift/Frontend/FrontendInputsAndOutputs.h"
2020
#include "swift/Frontend/InputFile.h"
21+
#include "clang/CAS/CASOptions.h"
2122
#include "llvm/ADT/Hashing.h"
2223
#include "llvm/ADT/Optional.h"
2324
#include "llvm/ADT/StringMap.h"
24-
#include "clang/CAS/CASOptions.h"
25+
#include "llvm/MC/MCTargetOptions.h"
2526

2627
#include <set>
2728
#include <string>
@@ -149,6 +150,15 @@ class FrontendOptions {
149150
/// CacheKey for input file.
150151
std::string InputFileKey;
151152

153+
/// Enable using the LLVM MCCAS backend for object file output.
154+
bool UseCASBackend = false;
155+
156+
/// The output mode for the CAS Backend.
157+
llvm::CASBackendMode CASObjMode;
158+
159+
/// Emit a .casid file next to the object file if CAS Backend is used.
160+
bool EmitCASIDFile = false;
161+
152162
/// Number of retry opening an input file if the previous opening returns
153163
/// bad file descriptor error.
154164
unsigned BadFileDescriptorRetryCount = 0;

Diff for: include/swift/Option/Options.td

+13
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,19 @@ def external_plugin_path : Separate<["-"], "external-plugin-path">, Group<plugin
18661866
Flags<[FrontendOption, ArgumentIsPath, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
18671867
HelpText<"Add directory to the plugin search path with a plugin server executable">,
18681868
MetaVarName<"<path>#<plugin-server-path>">;
1869+
1870+
def cas_backend: Flag<["-"], "cas-backend">,
1871+
Flags<[FrontendOption, NoDriverOption]>,
1872+
HelpText<"Enable using CASBackend for object file output">;
1873+
1874+
def cas_backend_mode: Joined<["-"], "cas-backend-mode=">,
1875+
Flags<[FrontendOption, NoDriverOption]>,
1876+
HelpText<"CASBackendMode for output kind">,
1877+
MetaVarName<"native|casid|verify">;
1878+
1879+
def cas_emit_casid_file: Flag<["-"], "cas-emit-casid-file">,
1880+
Flags<[FrontendOption, NoDriverOption]>,
1881+
HelpText<"Emit .casid file next to object file when CAS Backend is enabled">;
18691882

18701883
def load_plugin_library:
18711884
Separate<["-"], "load-plugin-library">, Group<plugin_search_Group>,

Diff for: include/swift/Subsystems.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ namespace swift {
263263
const IRGenOptions &opts,
264264
UnifiedStatsReporter *stats, DiagnosticEngine &diags,
265265
llvm::raw_pwrite_stream &out,
266-
llvm::sys::Mutex *diagMutex = nullptr);
266+
llvm::sys::Mutex *diagMutex = nullptr,
267+
llvm::raw_pwrite_stream *casid = nullptr);
267268

268269
/// Wrap a serialized module inside a swift AST section in an object file.
269270
void createSwiftModuleObjectFile(SILModule &SILMod, StringRef Buffer,

Diff for: lib/Frontend/ArgsToFrontendOptionsConverter.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,17 @@ bool ArgsToFrontendOptionsConverter::convert(
382382
Opts.BlocklistConfigFilePaths.push_back(A);
383383
}
384384

385+
if (Arg *A = Args.getLastArg(OPT_cas_backend_mode)) {
386+
Opts.CASObjMode = llvm::StringSwitch<llvm::CASBackendMode>(A->getValue())
387+
.Case("native", llvm::CASBackendMode::Native)
388+
.Case("casid", llvm::CASBackendMode::CASID)
389+
.Case("verify", llvm::CASBackendMode::Verify)
390+
.Default(llvm::CASBackendMode::Native);
391+
}
392+
393+
Opts.UseCASBackend = Args.hasArg(OPT_cas_backend);
394+
Opts.EmitCASIDFile = Args.hasArg(OPT_cas_emit_casid_file);
395+
385396
return false;
386397
}
387398

Diff for: lib/Frontend/CompilerInvocation.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ setIRGenOutputOptsFromFrontendOptions(IRGenOptions &IRGenOpts,
266266
}
267267
}(FrontendOpts.RequestedAction);
268268

269+
IRGenOpts.UseCASBackend = FrontendOpts.UseCASBackend;
270+
IRGenOpts.CASObjMode = FrontendOpts.CASObjMode;
271+
IRGenOpts.EmitCASIDFile = FrontendOpts.EmitCASIDFile;
272+
269273
// If we're in JIT mode, set the requisite flags.
270274
if (FrontendOpts.RequestedAction == FrontendOptions::ActionType::Immediate) {
271275
IRGenOpts.UseJIT = true;
@@ -1457,8 +1461,7 @@ static bool ValidateModulesOnceOptions(const ClangImporterOptions &Opts,
14571461
return false;
14581462
}
14591463

1460-
static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
1461-
ArgList &Args,
1464+
static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
14621465
DiagnosticEngine &Diags,
14631466
StringRef workingDirectory,
14641467
const LangOptions &LangOpts,

Diff for: lib/Frontend/Frontend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
414414

415415
bool CompilerInstance::setupCASIfNeeded(ArrayRef<const char *> Args) {
416416
const auto &Opts = getInvocation().getFrontendOptions();
417-
if (!Opts.EnableCaching)
417+
if (!getInvocation().requiresCAS())
418418
return false;
419419

420420
auto MaybeDB= Opts.CASOpts.getOrCreateDatabases();

Diff for: lib/FrontendTool/FrontendTool.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ static bool generateCode(CompilerInstance &Instance, StringRef OutputFilename,
16741674
std::unique_ptr<llvm::TargetMachine> TargetMachine =
16751675
createTargetMachine(opts, Instance.getASTContext());
16761676

1677+
TargetMachine->Options.MCOptions.CAS = Instance.getSharedCASInstance();
16771678
// Free up some compiler resources now that we have an IRModule.
16781679
freeASTContextIfPossible(Instance);
16791680

Diff for: lib/IRGen/IRGen.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ swift::getIRTargetOptions(const IRGenOptions &Opts, ASTContext &Ctx) {
128128
TargetOpts.DebuggerTuning = llvm::DebuggerKind::LLDB;
129129
TargetOpts.FunctionSections = Opts.FunctionSections;
130130

131+
// Set option to UseCASBackend if CAS was enabled on the command line.
132+
TargetOpts.UseCASBackend = Opts.UseCASBackend;
133+
134+
// Set option to select the CASBackendMode.
135+
TargetOpts.MCOptions.CASObjMode = Opts.CASObjMode;
136+
131137
auto *Clang = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
132138

133139
// WebAssembly doesn't support atomics yet, see
@@ -605,17 +611,31 @@ bool swift::performLLVM(const IRGenOptions &Opts,
605611
if (OutputFilename.empty())
606612
return false;
607613

614+
std::unique_ptr<raw_fd_ostream> CASIDFile;
615+
if (Opts.UseCASBackend && Opts.EmitCASIDFile &&
616+
Opts.CASObjMode != llvm::CASBackendMode::CASID &&
617+
Opts.OutputKind == IRGenOutputKind::ObjectFile && OutputFilename != "-") {
618+
std::string OutputFilenameCASID = std::string(OutputFilename);
619+
OutputFilenameCASID.append(".casid");
620+
std::error_code EC;
621+
CASIDFile = std::make_unique<raw_fd_ostream>(OutputFilenameCASID, EC);
622+
if (EC) {
623+
diagnoseSync(Diags, DiagMutex, SourceLoc(), diag::error_opening_output,
624+
OutputFilename, std::move(EC.message()));
625+
return true;
626+
}
627+
}
628+
608629
return compileAndWriteLLVM(Module, TargetMachine, Opts, Stats, Diags,
609-
*OutputFile, DiagMutex);
630+
*OutputFile, DiagMutex,
631+
CASIDFile ? CASIDFile.get() : nullptr);
610632
}
611633

612-
bool swift::compileAndWriteLLVM(llvm::Module *module,
613-
llvm::TargetMachine *targetMachine,
614-
const IRGenOptions &opts,
615-
UnifiedStatsReporter *stats,
616-
DiagnosticEngine &diags,
617-
llvm::raw_pwrite_stream &out,
618-
llvm::sys::Mutex *diagMutex) {
634+
bool swift::compileAndWriteLLVM(
635+
llvm::Module *module, llvm::TargetMachine *targetMachine,
636+
const IRGenOptions &opts, UnifiedStatsReporter *stats,
637+
DiagnosticEngine &diags, llvm::raw_pwrite_stream &out,
638+
llvm::sys::Mutex *diagMutex, llvm::raw_pwrite_stream *casid) {
619639

620640
// Set up the final code emission pass. Bitcode/LLVM IR is emitted as part of
621641
// the optimization pass pipeline.
@@ -639,8 +659,8 @@ bool swift::compileAndWriteLLVM(llvm::Module *module,
639659
EmitPasses.add(createTargetTransformInfoWrapperPass(
640660
targetMachine->getTargetIRAnalysis()));
641661

642-
bool fail = targetMachine->addPassesToEmitFile(EmitPasses, out, nullptr,
643-
FileType, !opts.Verify);
662+
bool fail = targetMachine->addPassesToEmitFile(
663+
EmitPasses, out, nullptr, FileType, !opts.Verify, nullptr, casid);
644664
if (fail) {
645665
diagnoseSync(diags, diagMutex, SourceLoc(),
646666
diag::error_codegen_init_fail);

Diff for: test/CAS/mccas.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-backend-mode=verify -cas-path %t/cas -o %t/test-verify.o
4+
// RUN: %llvm-dwarfdump %t/test-verify.o | %FileCheck %s --check-prefix=VERIFY-FILE
5+
// VERIFY-FILE: .debug_info
6+
7+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-backend-mode=native -cas-path %t/cas -o %t/test-native.o
8+
// RUN: %llvm-dwarfdump %t/test-native.o | %FileCheck %s --check-prefix=NATIVE-FILE
9+
// NATIVE-FILE: .debug_info
10+
11+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-backend-mode=casid -cas-path %t/cas -o %t/test-casid.id
12+
// RUN: cat %t/test-casid.id | %FileCheck %s --check-prefix=CASID-FILE
13+
// CASID-FILE: llvmcas://{{.*}}
14+
15+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-emit-casid-file -cas-backend-mode=verify -cas-path %t/cas -o %t/test-verify-emit.o
16+
// RUN: cat %t/test-verify-emit.o.casid | %FileCheck %s --check-prefix=VERIFY-EMIT
17+
// VERIFY-EMIT: llvmcas://{{.*}}
18+
19+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-emit-casid-file -cas-backend-mode=native -cas-path %t/cas -o %t/test-native-emit.o
20+
// RUN: cat %t/test-native-emit.o.casid | %FileCheck %s --check-prefix=NATIVE-EMIT
21+
// NATIVE-EMIT: llvmcas://{{.*}}
22+
23+
// RUN: %target-swift-frontend -c %s -g -cas-backend -cas-emit-casid-file -cas-backend-mode=casid -cas-path %t/cas -o %t/test.id
24+
// RUN: not cat %t/test.id.casid
25+
26+
func testFunc() {}

0 commit comments

Comments
 (0)