Skip to content

IRGen: add an option -min-valid-pointer-value to override the target's LeastValidPointerValue #81935

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

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ ERROR(objc_with_embedded,none,
"Objective-C interoperability cannot be enabled with embedded Swift.", ())
ERROR(no_allocations_without_embedded,none,
"-no-allocations is only applicable with embedded Swift.", ())
ERROR(min_ptr_value_without_embedded,none,
"-min-valid-pointer-value is only applicable with embedded Swift.", ())
ERROR(no_swift_sources_with_embedded,none,
"embedded swift cannot be enabled in a compiler built without Swift sources", ())

Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/IRGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ class IRGenOptions {
/// Pointer authentication.
PointerAuthOptions PointerAuth;

// If not 0, this overrides the value defined by the target.
uint64_t CustomLeastValidPointerValue = 0;

/// The different modes for dumping IRGen type info.
enum class TypeInfoDumpFilter {
All,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,10 @@ def enable_cond_fail_message_annotation : Flag<["-"], "enable-cond-fail-message-
def disable_cond_fail_message_annotation : Flag<["-"], "dissable-cond-fail-message-annotation">,
HelpText<"Disable cond_fail message annotation.">;

def min_valid_pointer_value : Joined<["-"], "min-valid-pointer-value=">,
MetaVarName<"<value>">,
HelpText<"Overrides the target's least valid pointer value.'">;

let Flags = [FrontendOption, NoDriverOption, HelpHidden, ModuleInterfaceOptionIgnorable] in {
def enable_pack_metadata_stack_promotion :
Joined<["-"], "enable-pack-metadata-stack-promotion=">,
Expand Down
15 changes: 15 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3506,6 +3506,21 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,

Opts.DebugInfoForProfiling |= Args.hasArg(OPT_debug_info_for_profiling);


if (const Arg *A = Args.getLastArg(OPT_min_valid_pointer_value)) {
// The LeastValidPointerValue is hard-coded in the runtime. Therefore it
// can only safely customized in embedded swift - which doesn't have a runtime.
if (!LangOpts.hasFeature(Feature::Embedded)) {
Diags.diagnose(SourceLoc(), diag::min_ptr_value_without_embedded);
return true;
}
if (StringRef(A->getValue()).getAsInteger(0, Opts.CustomLeastValidPointerValue)) {
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
A->getAsString(Args), A->getValue());
return true;
}
}

Opts.PrintInlineTree |= Args.hasArg(OPT_print_llvm_inline_tree);
// Always producing all outputs when caching is enabled.
Opts.AlwaysCompile |= Args.hasArg(OPT_always_compile_output_files) ||
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/SwiftTargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ SwiftTargetInfo SwiftTargetInfo::get(IRGenModule &IGM) {
break;
}

if (IGM.getOptions().CustomLeastValidPointerValue != 0)
target.LeastValidPointerValue = IGM.getOptions().CustomLeastValidPointerValue;

return target;
}

Expand Down
21 changes: 21 additions & 0 deletions test/embedded/min-pointer-value-option.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// RUN: %target-swift-emit-ir %s -module-name main -parse-as-library -enable-experimental-feature Embedded -O -min-valid-pointer-value=0x200 | %FileCheck %s

// REQUIRES: OS=macosx || OS=linux-gnu
// REQUIRES: swift_feature_Embedded

public func testit(_ s: S?) -> Bool {
return s != nil
}

class C {}

public struct S {
var a = InlineArray<57, UInt8>(repeating: 0)
var b = C()
var c = InlineArray<49, UInt8>(repeating: 0)
}

// CHECK-LABEL: define {{.*}} @"$e4main1SVSgWOg"(ptr %0)
// CHECK: icmp {{.*}}, 511
// CHECK-LABEL: }