Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #_objectFileFormat compilation conditional #80212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kubamracek
Copy link
Contributor

This implements the relevant part of the "Section Placement" proposal (draft: https://github.com/kubamracek/swift-evolution/blob/section-placement-control/proposals/0nnn-section-control.md, forum thread: https://forums.swift.org/t/pitch-3-section-placement-control/77435). Namely the ability to conditionalize on the object file format that the compiler is targeting (MachO, ELF, COFF of Wasm).

Adding with an underscored name (#_objectFileFormat) until the proposal is codified.

rdar://147505228

@kubamracek
Copy link
Contributor Author

@swift-ci please test

Comment on lines 654 to 677
// Set the "_objectFileFormat" platform condition.
if (Target.isOSBinFormatMachO()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "MachO");
} else if (Target.isOSBinFormatELF()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "ELF");
} else if (Target.isOSBinFormatCOFF()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF");
} else if (Target.isOSBinFormatWasm()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rewrite this as:

addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
  llvm::StringSwitch<llvm::Triple::ObjectFormatType>(Target.getObjectFormat())
    .Case(llvm::Triple::COFF, "COFF")
    .Case(llvm::Triple::ELF, "ELF")
    .Case(llvm::Triple::MachO, "MachO")
    .Case(llvm::Triple::Wasm, "Wasm"));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, not really, because we're not switching over strings here, but over the enum cases... I could do a regular switch, but that reads worse than the chain of ifs:

  StringRef FileFormatName;
  switch (Target.getObjectFormat()) {
  case llvm::Triple::COFF:
    FileFormatName = "COFF";
    break;
  case llvm::Triple::ELF:
    FileFormatName = "ELF";
    break;
  case llvm::Triple::MachO:
    FileFormatName = "MachO";
    break;
  case llvm::Triple::Wasm:
    FileFormatName = "Wasm";
    break;
  default:
    break;
  }
  if (!FileFormatName.empty()) {
    addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat,
                              FileFormatName);
  }

@kateinoigakukun
Copy link
Member

Should we consider gating this behind an experimental feature flag?

@kubamracek
Copy link
Contributor Author

I'm happy to do that if we deem that necessary, though all the other existing not-yet-ratified compilation conditions (e.g. pointerBitWidth) don't do that and just use an underscore.

@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3027

@swift-ci please test

@grynspan
Copy link
Contributor

Should we consider gating this behind an experimental feature flag?

Swift Testing will need to make use of this to support Embedded Swift, and requiring an experimental flag will make it more difficult to do so. (When it gets formally featurized, we can eagerly adopt the final version.)

addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF");
} else if (Target.isOSBinFormatWasm()) {
addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an else clause here that diagnoses the target is unknown? That way if we add a new port, we'll know to update this code.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3027

@swift-ci please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants