Skip to content
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

[DeviceSanitizer] Support detecting out-of-bounds error on DeviceGlobals #12753

Merged
merged 15 commits into from
Feb 29, 2024

Conversation

zhaomaosu
Copy link
Contributor

@zhaomaosu zhaomaosu commented Feb 19, 2024

Copy link
Contributor

@AllanZyne AllanZyne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Please modify UNIFIED_RUNTIME_REPO and UNIFIED_RUNTIME_TAG in "llvm/sycl/plugins/unified_runtime/CMakeLists.txt" to enable integration test.

@jinge90
Copy link
Contributor

jinge90 commented Feb 19, 2024

Hi, @zhaomaosu
Does this patch support following scenario:
clang++ -fsycl -fsanitize=address -c user_code1.cpp -o user1.o
clang++ -fsycl -fsanitize=address -c user_code2.cpp -o user2.o
clang++ -fsycl user1.o user2.o -o user.exe
Both user_code1.cpp and user_code2.cpp include device globals.
Thanks.

@zhaomaosu zhaomaosu marked this pull request as draft February 19, 2024 05:48
*Remove attribute of device globals in libdevice
@jinge90
Copy link
Contributor

jinge90 commented Feb 20, 2024

Hi, @zhaomaosu Does this patch support following scenario: clang++ -fsycl -fsanitize=address -c user_code1.cpp -o user1.o clang++ -fsycl -fsanitize=address -c user_code2.cpp -o user2.o clang++ -fsycl user1.o user2.o -o user.exe Both user_code1.cpp and user_code2.cpp include device globals. Thanks.

Hi, @zhaomaosu
Could you add some tests to cover this scenario?
Thanks.

@zhaomaosu
Copy link
Contributor Author

Hi, @zhaomaosu Could you add some tests to cover this scenario? Thanks.

Ok, I'll add tests for it.

@zhaomaosu
Copy link
Contributor Author

Hi, @zhaomaosu Could you add some tests to cover this scenario? Thanks.

Done

@zhaomaosu
Copy link
Contributor Author

Hi @steffenlarsen, the PR in UR repo has been merged. I updated the UR tag. May I get your approval?

Copy link
Contributor

@steffenlarsen steffenlarsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@steffenlarsen
Copy link
Contributor

Review still required from @intel/unified-runtime-reviewers & @intel/dpcpp-tools-reviewers. Friendly ping.

SmallVector<Constant *, 8> DeviceGlobalMetadata;

constexpr uint64_t MaxRZ = 1 << 18;
const uint64_t MinRZ = 32;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const uint64_t MinRZ = 32;
constexpr uint64_t MinRZ = 32;

Nit, for uniformity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const uint64_t MinRZ = 32;

Type *IntptrTy =
Type::getIntNTy(M.getContext(), M.getDataLayout().getPointerSizeInBits());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Type::getIntNTy(M.getContext(), M.getDataLayout().getPointerSizeInBits());
Type::getIntNTy(M.getContext(), DL.getPointerSizeInBits());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

constexpr uint64_t MaxRZ = 1 << 18;
const uint64_t MinRZ = 32;

Type *IntptrTy =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Type *IntptrTy =
Type *IntTy =

Or maybe SizeTTy. That type is not a pointer, so ptr is definitely confusing here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to IntTy

// Non image scope device globals are implemented by device USM, and the
// out-of-bounds check for them will be done by sanitizer USM part. So we
// exclude them here.
if (isDeviceGlobalVariable(G) && hasDeviceImageScopeProperty(G)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be turned into an early exit instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


assert((RZ + SizeInBytes) % MinRZ == 0);
return RZ;
}();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting seems to be broken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-run clang-format

} else {
// Calculate RZ, where MinRZ <= RZ <= MaxRZ, and RZ ~ 1/4 *
// SizeInBytes.
RZ = std::clamp((SizeInBytes / MinRZ / 4) * MinRZ, MinRZ, MaxRZ);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I right that (SizeInBytes / MinRZ / 4) * MinRZ is written this way on purpose, to ensure certain rounding during calculation? If so, a comment would be welcome about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is coming from https://github.com/intel/llvm/blob/sycl/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp#L2767.
I traced back the git histories. It seems that this code initially started out like this. So, I also don't know if it's written this way for some reason. Sorry for this.
But we do this instrumentation for device global in sycl-post-link tool temporarily. When unified runtime implemented the API urProgramGetGlobalVariablePointer, I'll move this instrumentation code back to AddressSanitizer pass.

@zhaomaosu
Copy link
Contributor Author

@maksimsab, any other comments?

@kbenzie
Copy link
Contributor

kbenzie commented Feb 29, 2024

@maksimsab if the changes you requested are sufficient please approve the PR.

@intel/dpcpp-esimd-reviewers please review this ASAP, this PR is blocking any progress of merging other UR changes.

This is affecting multiple teams. Any help to expedite the approval and merging of this PR would be greatly appriciated.

@maksimsab
Copy link
Contributor

I am OK to let this PR go in order to unblock other teams.
Currently, the new Pass has 0 test coverage. @zhaomaosu Are you ok to address this in the follow-up PR?

Copy link
Contributor

@maksimsab maksimsab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhaomaosu
Copy link
Contributor Author

I am OK to let this PR go in order to unblock other teams. Currently, the new Pass has 0 test coverage. @zhaomaosu Are you ok to address this in the follow-up PR?

Sure, I'll do this later. Thank you.

@zhaomaosu
Copy link
Contributor Author

Can anyone help merge this PR? Thanks a lot.

Copy link
Contributor

@sarnex sarnex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esimd lgtm, leaving tools for others

@steffenlarsen steffenlarsen merged commit a14cfdd into intel:sycl Feb 29, 2024
12 checks passed
@zhaomaosu zhaomaosu deleted the sanitizer-device-global branch March 1, 2024 03:37
steffenlarsen pushed a commit that referenced this pull request Mar 1, 2024
Tests HIP Support for Graph from
oneapi-src/unified-runtime#1254 and updates
documentation.

Depends on #12753.

---------

Co-authored-by: Andrey Alekseenko <andrey.alekseenko@scilifelab.se>
Co-authored-by: Ewan Crawford <ewan@codeplay.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants