Skip to content

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

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);
  }

Copy link
Member

Choose a reason for hiding this comment

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

The covered switch would make it easier to identify new cases though I think. It is more that I am after a switch rather than a series of cascading if conditions.

@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.)

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

@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3027

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@grynspan
Copy link
Contributor

grynspan commented Mar 30, 2025

Are there any other object formats that LLVM supports? Should we add cases for them even if Swift itself hasn't added toolchain support?

Edit: The following are all the cases in the object format enum in the Swift repo:

  enum ObjectFormatType {
    UnknownObjectFormat,

    COFF,
    DXContainer,
    ELF,
    GOFF,
    MachO,
    SPIRV,
    Wasm,
    XCOFF,
  };

So it may make sense to just switch over the whole enum (with getObjectFormat()).

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

Are there any other object formats that LLVM supports? Should we add cases for them even if Swift itself hasn't added toolchain support?

Edit: The following are all the cases in the object format enum in the Swift repo:

  enum ObjectFormatType {
    UnknownObjectFormat,

    COFF,
    DXContainer,
    ELF,
    GOFF,
    MachO,
    SPIRV,
    Wasm,
    XCOFF,
  };

So it may make sense to just switch over the whole enum (with getObjectFormat()).

I think it's probably undesirable to go beyond the 4 object file formats that Swift targets today, because (1) we'd have to lock down the specific spelling of those and (2) it would suggest that the compiler is actually capable of producing valid object files in these formats, but that's almost certainly not true until someone actively builds that support.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test macOS platform

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@compnerd
Copy link
Member

compnerd commented Apr 8, 2025

it would suggest that the compiler is actually capable of producing valid object files in these formats, but that's almost certainly not true until someone actively builds that support.

I don't know if I agree here. However, we do in a number of places currently just abort if you use a different object file format. Some of these are unlikely to ever be supported (e.g. DXContainer and SPIRV), so adding the full coverage really doesn't add much value, but does add noise.

@kubamracek kubamracek changed the title Add #_objectFileFormat compilation conditional Add #objectFormat compilation conditional (SE-0492) Oct 16, 2025
@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3170
@swift-ci please test

PLATFORM_CONDITION_(HasAtomicBitWidth, "hasAtomicBitWidth")

/// The active target's file format (Mach-O, ELF, COFF, WASM)
PLATFORM_CONDITION_(ObjectFileFormat, "objectFormat")
Copy link
Member

Choose a reason for hiding this comment

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

No need for the underscore here?

// RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-msvc -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target arm64-apple-macos

#if objectFormat(MachO) || objectFormat(ELF) || objectFormat(Wasm) || objectFormat(COFF)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be good to have this check each one separately and somehow have each RUN line check that it selected the right one. This would detect it if someone managed to accidentally make it always be true or always false.

@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3170
@swift-ci please test

@kubamracek
Copy link
Contributor Author

swiftlang/swift-syntax#3170
@swift-ci please test macOS platform

}

func isActiveTargetObjectFormat(name: String) throws -> Bool {
try staticBuildConfiguration.isActiveTargetObjectFormat(name: name)
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't really matter, but I would expect this never to throw, because it's just querying the static build configuration

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek enabled auto-merge October 23, 2025 14:35
@kubamracek kubamracek merged commit 66eef68 into swiftlang:main Oct 23, 2025
4 of 5 checks passed
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.

6 participants