Add support for an assert_configuration builtin function

This patch adds support for a builtin function assert_configuration that is
replaced by constant progpagation by an appropriate value dependent on a compile
time setting. This replacement can also be disabled when serializing sil for a
library.

Using this mechanism we implement assertions that can  be disabled (or whose
behavior changes) depending on compile time build settings (Debug, Release,
DisableReplacement).

In the standard library we can now write one assert function that uses this
builtin function to provide different compile time selectable runtime behavior.

Example

Assert.swift:

@transparent
func assert<T : LogicValue>(
  condition: @auto_closure () -> T, message: StaticString = StaticString(),

  // Do not supply these parameters explicitly; they will be filled in
  // by the compiler and aren't even present when asserts are disabled
  file: StaticString = __FILE__, line: UWord = __LINE__
) {
  // Only in debug mode.
  if _isDebug() {
    assert(condition().getLogicValue(), message, file, line)
  }
}

AssertCommon.swift:

@transparent
func _isDebug() -> Bool {
  return Int32(Builtin.assert_configuration()) == 0;
}

rdar://16458612

Swift SVN r16472
This commit is contained in:
Arnold Schwaighofer
2014-04-17 22:05:42 +00:00
parent 527a3149a8
commit 989d554a45
11 changed files with 239 additions and 22 deletions

View File

@@ -198,6 +198,10 @@ VerifyMode("verify",
llvm::cl::desc("verify diagnostics against expected-"
"{error|warning|note} annotations"));
static llvm::cl::opt<unsigned>
AssertConfId("assert-conf-id", llvm::cl::Hidden,
llvm::cl::init(0));
static llvm::cl::opt<unsigned>
SILInlineThreshold("sil-inline-threshold", llvm::cl::Hidden,
llvm::cl::init(50));
@@ -325,6 +329,7 @@ int main(int argc, char **argv) {
SILOpts.VerifyAll = EnableSILVerifyAll;
SILOpts.PrintAll = EnableSILPrintAll;
SILOpts.RemoveRuntimeAsserts = RemoveRuntimeAsserts;
SILOpts.AssertConfig = AssertConfId;
SILPassManager PM(CI.getSILModule(), SILOpts);
PM.registerAnalysis(createCallGraphAnalysis(CI.getSILModule()));