Skip to content

[docs][nfc] Fix code-formatting in UB docs #139079

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
May 8, 2025
Merged
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: 10 additions & 8 deletions llvm/docs/UndefinedBehavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ Take the following example function:
entry:
br label %loop

loop:
loop:
%i = phi i32 [ 0, %entry ], [ %i2, %loop.end ]
%cond = icmp ule i32 %i, %n
br i1 %cond, label %loop.cont, label %exit

loop.cont:
loop.cont:
br i1 %c, label %then, label %else

then:
Expand Down Expand Up @@ -334,22 +334,22 @@ We would obtain the following IR:
entry:
br i1 %c, label %then, label %else

then:
then:
%i = phi i32 [ 0, %entry ], [ %i2, %then.cont ]
%cond = icmp ule i32 %i, %n
br i1 %cond, label %then.cont, label %exit

then.cont:
then.cont:
...
%i2 = add i32 %i, 1
br label %then

else:
else:
%i3 = phi i32 [ 0, %entry ], [ %i4, %else.cont ]
%cond = icmp ule i32 %i3, %n
br i1 %cond, label %else.cont, label %exit

else.cont:
else.cont:
...
%i4 = add i32 %i3, 1
br label %else
Expand Down Expand Up @@ -381,6 +381,8 @@ We can make the loop unswitching optimization above correct as follows:
entry:
%c2 = freeze i1 %c
br i1 %c2, label %then, label %else
...
}


Writing Tests Without Undefined Behavior
Expand All @@ -399,7 +401,7 @@ particular divisor value because our optimization kicks in regardless:
define i32 @fn(i8 %a) {
%div = udiv i8 %a, poison
...
}
}

The issue with this test is that it triggers immediate UB. This prevents
verification tools like Alive from validating the correctness of the
Expand All @@ -412,7 +414,7 @@ The test above should use a dummy function argument instead of using poison:
define i32 @fn(i8 %a, i8 %dummy) {
%div = udiv i8 %a, %dummy
...
}
}

Common sources of immediate UB in tests include branching on undef/poison
conditions and dereferencing undef/poison/null pointers.
Expand Down
Loading