forked from realtime-sanitizer/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
[DRAFT] Loop exploration function pass #7
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
Open
cjappl
wants to merge
7
commits into
main
Choose a base branch
from
loop_exploration_function_pass
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15821d0
First version
cjappl 0a02dcd
WIP: Loop pass
cjappl 5f4d0b0
Cleanup
cjappl b86f26a
Fix incorrect conditional predicting loop count
cjappl 2e8aef7
Fix clang format
cjappl b31279d
working with hack
cjappl 428d2ec
Fix clang format
cjappl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O3 | ||
// RUN: %run %t 2>&1 | FileCheck %s --allow-empty | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O2 | ||
// RUN: %run %t 2>&1 | FileCheck %s --allow-empty | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O1 | ||
// RUN: %run %t 2>&1 | FileCheck %s --allow-empty | ||
|
||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O0 | ||
// RUN: %run %t 2>&1 | FileCheck %s --allow-empty | ||
|
||
// UNSUPPORTED: ios | ||
|
||
// Intent: Ensure basic bound audio loops don't trigger rtsan. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <assert.h> | ||
|
||
void FillBufferInterleaved(int *buffer, int sample_count, int channel_count) | ||
[[clang::nonblocking]] { | ||
for (int sample = 0; sample < sample_count; sample++) | ||
for (int channel = 0; channel < channel_count; channel++) | ||
buffer[sample * channel_count + channel] = sample; | ||
} | ||
|
||
void Deinterleave(int *buffer, int sample_count, int channel_count, | ||
int *scratch_buffer) [[clang::nonblocking]] { | ||
for (int channel = 0; channel < channel_count; channel++) | ||
for (int sample = 0; sample < sample_count; sample++) { | ||
int interleaved_index = sample * channel_count + channel; | ||
int deinterleaved_index = channel * sample_count + sample; | ||
scratch_buffer[deinterleaved_index] = buffer[interleaved_index]; | ||
} | ||
|
||
for (int i = 0; i < sample_count * channel_count; i++) | ||
buffer[i] = scratch_buffer[i]; | ||
} | ||
|
||
int main() { | ||
const int sample_count = 10; | ||
const int channel_count = 2; | ||
int buffer[channel_count * sample_count]; | ||
|
||
FillBufferInterleaved(buffer, sample_count, channel_count); | ||
|
||
assert(buffer[0] == 0); | ||
assert(buffer[1] == 0); | ||
assert(buffer[2] == 1); | ||
assert(buffer[3] == 1); | ||
|
||
assert(buffer[18] == 9); | ||
assert(buffer[19] == 9); | ||
|
||
int scratch_buffer[channel_count * sample_count]; | ||
|
||
Deinterleave(buffer, sample_count, channel_count, scratch_buffer); | ||
|
||
assert(buffer[0] == 0); | ||
assert(buffer[1] == 1); | ||
assert(buffer[8] == 8); | ||
assert(buffer[9] == 9); | ||
|
||
assert(buffer[10] == 0); | ||
assert(buffer[11] == 1); | ||
assert(buffer[18] == 8); | ||
assert(buffer[19] == 9); | ||
|
||
return 0; | ||
} | ||
|
||
// CHECK-NOT: {{.*Real-time violation.*}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s | ||
// UNSUPPORTED: ios | ||
|
||
// Intent: Ensure we detect infinite loops | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void infinity() [[clang::nonblocking]] { | ||
while (true) | ||
; | ||
} | ||
|
||
int main() { | ||
infinity(); | ||
return 0; | ||
// CHECK: {{.*Real-time violation.*}} | ||
// CHECK: {{.*infinity*}} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// RUN: %clangxx -DCAS_SPINLOCK -fsanitize=realtime %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ALL --check-prefix=CHECK-CAS | ||
// RUN: %clangxx -DTEST_AND_SET_SPINLOCK -fsanitize=realtime %s -o %t | ||
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-ALL --check-prefix=CHECK-TEST-AND-SET | ||
// UNSUPPORTED: ios | ||
|
||
#include <atomic> | ||
|
||
#include <atomic> | ||
|
||
class SpinLockTestAndSet { | ||
public: | ||
void lock() { | ||
while (lock_flag.test_and_set(std::memory_order_acquire)) { | ||
// Busy-wait (spin) until the lock is acquired | ||
} | ||
} | ||
|
||
void unlock() { lock_flag.clear(std::memory_order_release); } | ||
|
||
private: | ||
std::atomic_flag lock_flag = ATOMIC_FLAG_INIT; | ||
}; | ||
|
||
class SpinLockCompareExchange { | ||
public: | ||
void lock() { | ||
bool expected = false; | ||
while (!lock_flag.compare_exchange_weak( | ||
expected, true, std::memory_order_acquire, std::memory_order_relaxed)) { | ||
} | ||
} | ||
|
||
void unlock() { lock_flag.store(false, std::memory_order_release); } | ||
|
||
private: | ||
std::atomic<bool> lock_flag{false}; | ||
}; | ||
|
||
int lock_violation() [[clang::nonblocking]] { | ||
#if defined(TEST_AND_SET_SPINLOCK) | ||
SpinLockTestAndSet lock; | ||
#elif defined(CAS_SPINLOCK) | ||
SpinLockCompareExchange lock; | ||
#else | ||
# error "No spinlock defined" | ||
#endif | ||
lock.lock(); | ||
return 0; | ||
} | ||
|
||
int main() [[clang::nonblocking]] { lock_violation(); } | ||
|
||
// CHECK-ALL: {{.*Real-time violation.*}} | ||
// CHECK-CAS: {{.*SpinLockCompareExchange::lock.*}} | ||
// CHECK-TEST-AND-SET: {{.*SpinLockTestAndSet::lock.*}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O3 | ||
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O2 | ||
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty | ||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O1 | ||
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty | ||
|
||
// RUN: %clangxx -fsanitize=realtime %s -o %t -O0 | ||
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty | ||
|
||
// UNSUPPORTED: ios | ||
|
||
// Intent: Ensure basic bound audio loops don't trigger rtsan. | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <assert.h> | ||
|
||
void BadScalarEvolution(int *buffer, int sample_count, int channel_count) | ||
[[clang::nonblocking]] { | ||
|
||
int sample = 0; | ||
while (sample < sample_count) { | ||
int channel = 0; | ||
while (channel < channel_count) { | ||
buffer[sample * channel_count + channel] = sample; | ||
channel++; | ||
} | ||
sample++; | ||
|
||
// NOTE! Here is the "bug" that causes the loop to be unbounded. | ||
sample_count++; | ||
} | ||
} | ||
|
||
int main() { | ||
const int sample_count = 10; | ||
const int channel_count = 2; | ||
int buffer[channel_count * sample_count]; | ||
|
||
BadScalarEvolution(buffer, sample_count, channel_count); | ||
|
||
return 0; | ||
} | ||
|
||
// CHECK: {{.*Real-time violation.*}} | ||
// CHECK-NEXT {{.*BadScalarEvolution.*}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
llvm/test/Instrumentation/RealtimeSanitizer/rtsan_bound_loop.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
; RUN: opt < %s -passes='sroa,rtsan' -S | FileCheck %s | ||
|
||
|
||
define void @procces(ptr noundef %buffer, i32 noundef %size) #0 { | ||
entry: | ||
%buffer.addr = alloca ptr, align 8 | ||
%size.addr = alloca i32, align 4 | ||
%i = alloca i32, align 4 | ||
store ptr %buffer, ptr %buffer.addr, align 8 | ||
store i32 %size, ptr %size.addr, align 4 | ||
store i32 0, ptr %i, align 4 | ||
br label %for.cond | ||
|
||
for.cond: ; preds = %for.inc, %entry | ||
%0 = load i32, ptr %i, align 4 | ||
%1 = load i32, ptr %size.addr, align 4 | ||
%cmp = icmp slt i32 %0, %1 | ||
br i1 %cmp, label %for.body, label %for.end | ||
|
||
for.body: ; preds = %for.cond | ||
%2 = load i32, ptr %i, align 4 | ||
%conv = sitofp i32 %2 to float | ||
%3 = load ptr, ptr %buffer.addr, align 8 | ||
%4 = load i32, ptr %i, align 4 | ||
%idxprom = sext i32 %4 to i64 | ||
%arrayidx = getelementptr inbounds float, ptr %3, i64 %idxprom | ||
store float %conv, ptr %arrayidx, align 4 | ||
br label %for.inc | ||
|
||
for.inc: ; preds = %for.body | ||
%5 = load i32, ptr %i, align 4 | ||
%inc = add nsw i32 %5, 1 | ||
store i32 %inc, ptr %i, align 4 | ||
br label %for.cond | ||
|
||
for.end: ; preds = %for.cond | ||
ret void | ||
} | ||
|
||
attributes #0 = { sanitize_realtime } | ||
|
||
; In this simple loop, we should not insert rtsan_expect_not_realtime | ||
; CHECK-NOT: call{{.*}}@__rtsan_expect_not_realtime |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is where we add the SROA pass