Skip to content

[Constant Evaluator][Test] Add tests to check whether fatal errors are detected #27353

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
Sep 26, 2019
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
18 changes: 14 additions & 4 deletions lib/SILOptimizer/UtilityPasses/ConstantEvaluableSubsetChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ class ConstantEvaluableSubsetChecker : public SILModuleTransform {
if (isa<ReturnInst>(inst))
break;

assert(!previousEvaluationHadFatalError &&
"cannot continue evaluation of test driver as previous call "
"resulted in non-skippable evaluation error.");

auto *applyInst = dyn_cast<ApplyInst>(inst);
SILFunction *callee = nullptr;
if (applyInst) {
Expand All @@ -89,6 +85,16 @@ class ConstantEvaluableSubsetChecker : public SILModuleTransform {

if (!applyInst || !callee ||
!callee->hasSemanticsAttr(constantEvaluableSemanticsAttr)) {

// Ignore these instructions if we had a fatal error already.
if (previousEvaluationHadFatalError) {
if (isa<TermInst>(inst)) {
assert(false && "non-constant control flow in the test driver");
}
++currI;
continue;
}

std::tie(nextInstOpt, errorVal) =
stepEvaluator.tryEvaluateOrElseMakeEffectsNonConstant(currI);
if (!nextInstOpt) {
Expand All @@ -100,6 +106,10 @@ class ConstantEvaluableSubsetChecker : public SILModuleTransform {
continue;
}

assert(!previousEvaluationHadFatalError &&
"cannot continue evaluation of test driver as previous call "
"resulted in non-skippable evaluation error.");

// Here, a function annotated as "constant_evaluable" is called.
llvm::errs() << "@" << demangleSymbolName(callee->getName()) << "\n";
std::tie(nextInstOpt, errorVal) =
Expand Down
27 changes: 27 additions & 0 deletions test/SILOptimizer/constant_evaluable_subset_test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ func interpretDivideByZero() -> Int {
return testDivideByZero(127, 0)
}

// CHECK-LABEL: @testDividingFullWidthByZero
// CHECK: error: not constant evaluable
@_semantics("constant_evaluable")
func testDividingFullWidthByZero(_ x: Int, _ y: Int, _ z: UInt) -> Int {
return x.dividingFullWidth((y, z)).1
} // CHECK: note: {{.*}}: Division by zero
// CHECK: note: operation performed during this call traps

@_semantics("test_driver")
func interpretDividingFullWidthByZero() -> Int {
return testDividingFullWidthByZero(0, 1, 1)
}

// CHECK-LABEL: @testDivideOverflow
// CHECK: error: not constant evaluable
@_semantics("constant_evaluable")
Expand All @@ -364,6 +377,20 @@ func interpretDivideOverflow() -> Int8 {
return testDivideOverflow(-128, -1)
}

// CHECK-LABEL: @testDistance
// CHECK: error: not constant evaluable
@_semantics("constant_evaluable")
func testDistance(_ x: UInt, _ y: UInt) -> Int {
return x.distance(to: y)
// CHECK: note: {{.*}}: Distance is not representable in Int
// CHECK: note: operation performed during this call traps
}

@_semantics("test_driver")
func interpretDistanceTest() -> Int {
return testDistance(0, UInt.max)
}

// CHECK-LABEL: @testInOut
// CHECK-NOT: error:
@_semantics("constant_evaluable")
Expand Down