<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/77739>77739</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Issue with CoroEarlyPass Pass Introducing New Undefined Behavior in LLVM Transformation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
wyanzhao
</td>
</tr>
</table>
<pre>
Consider that we have a source code file named `test.ll` with the following contents:
```llvm
define ptr @f(i32 %n) {
ret ptr null;
}
declare void @llvm.coro.resume(ptr)
define i32 @main() {
%hdl = call ptr @f(i32 4)
call void @llvm.coro.resume(ptr %hdl)
ret i32 0
}
```
After executing the command `llvm-project/build/bin/opt ./test.ll -passes='default<O2>' -print-after-all`, we observe the transformed code resulting from the `CoroEarlyPass` pass in the output:
```llvm
; *** IR Dump After CoroEarlyPass on [module] ***
; ModuleID = './test.ll'
source_filename = "./test.ll"
define ptr @f(i32 %n) {
ret ptr null
}
declare void @llvm.coro.resume(ptr)
define i32 @main() {
%hdl = call ptr @f(i32 4)
%0 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 0)
call fastcc void %0(ptr %hdl)
ret i32 0
}
; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: read)
declare ptr @llvm.coro.subfn.addr(ptr nocapture readonly, i8) #0
attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: read) }
```
### Issue Description
The `CoroEarlyPass` transformation introduces new instances of undefined behavior in the LLVM IR, particularly when handling functions that return a null pointer in the context of coroutine intrinsics.
### Original Code Behavior:
In the original code's main function, the function `@f(i32 %n)` returns `ptr null`.
The call to `@llvm.coro.resume(ptr %hdl)` with `%hdl` being a null pointer (returned from @f) leads to undefined behavior.
### Transformed Code Behavior:
In the transformed code's main function, `%hdl` (which is ptr null) is used in the expression` %0 = call ptr @llvm.coro.subfn.addr(ptr %hdl, i8 0)`. This call likely leads to undefined behavior due to the dereference of a null pointer.
The result of this call (`%0`) is considered to be "poisoned" due to the undefined behavior of the previous step.
Subsequently, the `call fastcc void %0(ptr %hdl)` with a potentially poisoned `%0` and a null `%hdl` introduces another instance of undefined behavior.
Thus, the `CoroEarlyPass` pass introduces new undefined behaviors in the transformed code, which were not present in the original source code.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVltv47YS_jX0yyCGQtmW9eCHJF4DAXbPHuzJ6WtBiSOLXYpUeYnX_fXFUPI18e4W7UMBIY7E4Vy-mflmhPdqaxBXbP7I5uuJiKG1brXbC_NHK-yksnK_erLGK4kOQisC7BBa8YogwNvoaoTaSoRGaQQjOpTAFllAH6Zas0UGOxVaCC1CY7W2O2W2UFsT0ATP8geWrVn2wBbZ8Gj92g2fJDbKIPTBAZtlDeNLlXNgfG4YL4EVj4MYgMOQpEzUmuXjZ1asx39GZbUWDuHVKknqyMy0ts5OHfrYIePLPjjGy8tLyYNkdpZ1QhnGl1fGGZ-3UgPL11ALra_dnR1VwnD-fQdGdWeXKDhSlF2HdQBseH1oAjrAb1jHQAAT3LXtOmFSNsjaXe_sb1gHxjdVVFrSL0W0sX2AKeObMWVw1wvv0bN8zXghsRFRB5Y_feYs_8B4AXe9UybcCTJ5J1KOGX-iqrCVR_eKyXpwwvjGOqqHVB8Up07ONc52SYYtsifr7Afh9P6_wnuqFjIOyqRzG0Mfw6lIbpUKyx-B8YfhgecvsI5dDwMmFwbAGmDzx87KqJHN16dbJ0Wf0uHzOuWU8eIMGsaLQW6o-1-p5KniR1F-IcrfKaW_WMz_1kpmfJ69ETw54mPVmKmQ0l1V9ROoJWTXLdEIH-p6DIjPsxutcLMTjpnbRFMHZQ08hOCIW8BYslCJ-isY2zhEMNbvTQ3GRrNTRsJOae0wRGegw866PeNL4bYddnTfoZBHDw7I_zheMtuH6DApsEbvh9gT4jzPzh0XIThVxYA-HQ2lVDz-U67DLcYY__J8eODZ-4iwRl871ROK52IvN5r12OQi4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