-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Offload] Introduce the offload sanitizer (initially for traps) #101417
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
jdoerfert
wants to merge
1
commit into
llvm:main
Choose a base branch
from
jdoerfert:pr/offload_sanitizer_traps
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.
+535
−26
Open
Changes from all commits
Commits
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
27 changes: 27 additions & 0 deletions
27
llvm/include/llvm/Transforms/Instrumentation/OffloadSanitizer.h
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,27 @@ | ||
| //===- Transforms/Instrumentation/OffloadSanitizer.h ------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Pass to instrument offload code in order to detect errors and communicate | ||
| // them to the LLVM/Offload runtimes. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_OFFLOADSAN_H | ||
| #define LLVM_TRANSFORMS_INSTRUMENTATION_OFFLOADSAN_H | ||
|
|
||
| #include "llvm/IR/PassManager.h" | ||
|
|
||
| namespace llvm { | ||
|
|
||
| class OffloadSanitizerPass : public PassInfoMixin<OffloadSanitizerPass> { | ||
| public: | ||
| PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); | ||
| }; | ||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_TRANSFORMS_INSTRUMENTATION_OFFLOADSAN_H |
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
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
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
150 changes: 150 additions & 0 deletions
150
llvm/lib/Transforms/Instrumentation/OffloadSanitizer.cpp
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,150 @@ | ||
| //===-- OffloadSanitizer.cpp - Offload sanitizer --------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/Transforms/Instrumentation/OffloadSanitizer.h" | ||
|
|
||
| #include "llvm/ADT/SetVector.h" | ||
| #include "llvm/ADT/SmallVector.h" | ||
| #include "llvm/IR/DebugInfoMetadata.h" | ||
| #include "llvm/IR/DerivedTypes.h" | ||
| #include "llvm/IR/IRBuilder.h" | ||
| #include "llvm/IR/InstIterator.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/IntrinsicInst.h" | ||
| #include "llvm/IR/Intrinsics.h" | ||
| #include "llvm/IR/IntrinsicsAMDGPU.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/IR/Value.h" | ||
| #include "llvm/Transforms/Utils/Cloning.h" | ||
| #include "llvm/Transforms/Utils/ModuleUtils.h" | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| #define DEBUG_TYPE "offload-sanitizer" | ||
|
|
||
| namespace { | ||
|
|
||
| class OffloadSanitizerImpl final { | ||
| public: | ||
| OffloadSanitizerImpl(Module &M, FunctionAnalysisManager &FAM) | ||
| : M(M), FAM(FAM), Ctx(M.getContext()) {} | ||
|
|
||
| bool instrument(); | ||
|
|
||
| private: | ||
| bool shouldInstrumentFunction(Function &Fn); | ||
| bool instrumentFunction(Function &Fn); | ||
| bool instrumentTrapInstruction(IntrinsicInst &II); | ||
|
|
||
| FunctionCallee getOrCreateFn(FunctionCallee &FC, StringRef Name, Type *RetTy, | ||
| ArrayRef<Type *> ArgTys) { | ||
| if (!FC) { | ||
| auto *NewAllocationFnTy = FunctionType::get(RetTy, ArgTys, false); | ||
| FC = M.getOrInsertFunction(Name, NewAllocationFnTy); | ||
| } | ||
| return FC; | ||
| } | ||
|
|
||
| /// void __offload_san_trap_info(Int64Ty); | ||
| FunctionCallee TrapInfoFn; | ||
| FunctionCallee getTrapInfoFn() { | ||
| return getOrCreateFn(TrapInfoFn, "__offload_san_trap_info", VoidTy, | ||
| {/*PC*/ Int64Ty}); | ||
| } | ||
|
|
||
| CallInst *createCall(IRBuilder<> &IRB, FunctionCallee Callee, | ||
| ArrayRef<Value *> Args = std::nullopt, | ||
| const Twine &Name = "") { | ||
| Calls.push_back(IRB.CreateCall(Callee, Args, Name)); | ||
| return Calls.back(); | ||
| } | ||
| SmallVector<CallInst *> Calls; | ||
|
|
||
| Value *getPC(IRBuilder<> &IRB) { | ||
| return IRB.CreateIntrinsic(Int64Ty, Intrinsic::amdgcn_s_getpc, {}, nullptr, | ||
| "PC"); | ||
| } | ||
|
|
||
| Module &M; | ||
| FunctionAnalysisManager &FAM; | ||
| LLVMContext &Ctx; | ||
|
|
||
| Type *VoidTy = Type::getVoidTy(Ctx); | ||
| Type *IntptrTy = M.getDataLayout().getIntPtrType(Ctx); | ||
| PointerType *PtrTy = PointerType::getUnqual(Ctx); | ||
| IntegerType *Int8Ty = Type::getInt8Ty(Ctx); | ||
| IntegerType *Int32Ty = Type::getInt32Ty(Ctx); | ||
| IntegerType *Int64Ty = Type::getInt64Ty(Ctx); | ||
|
|
||
| const DataLayout &DL = M.getDataLayout(); | ||
| }; | ||
|
|
||
| } // end anonymous namespace | ||
|
|
||
| bool OffloadSanitizerImpl::shouldInstrumentFunction(Function &Fn) { | ||
| if (Fn.isDeclaration()) | ||
| return false; | ||
| return !Fn.hasFnAttribute(Attribute::DisableSanitizerInstrumentation); | ||
| } | ||
|
|
||
| bool OffloadSanitizerImpl::instrumentTrapInstruction(IntrinsicInst &II) { | ||
| IRBuilder<> IRB(&II); | ||
| createCall(IRB, getTrapInfoFn(), {getPC(IRB)}); | ||
| return true; | ||
| } | ||
|
|
||
| bool OffloadSanitizerImpl::instrumentFunction(Function &Fn) { | ||
| if (!shouldInstrumentFunction(Fn)) | ||
| return false; | ||
|
|
||
| bool Changed = false; | ||
| for (auto &I : instructions(Fn)) { | ||
| switch (I.getOpcode()) { | ||
| case Instruction::Call: { | ||
| auto &CI = cast<CallInst>(I); | ||
| if (auto *II = dyn_cast<IntrinsicInst>(&CI)) | ||
| if (II->isNonContinuableTrap()) | ||
| Changed |= instrumentTrapInstruction(*II); | ||
| break; | ||
| } | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return Changed; | ||
| } | ||
|
|
||
| bool OffloadSanitizerImpl::instrument() { | ||
| bool Changed = false; | ||
|
|
||
| for (Function &Fn : M) | ||
| Changed |= instrumentFunction(Fn); | ||
|
|
||
| removeFromUsedLists(M, [&](Constant *C) { | ||
| if (!C->getName().starts_with("__offload_san")) | ||
| return false; | ||
| return Changed = true; | ||
| }); | ||
|
|
||
| return Changed; | ||
| } | ||
|
|
||
| PreservedAnalyses OffloadSanitizerPass::run(Module &M, | ||
| ModuleAnalysisManager &AM) { | ||
| FunctionAnalysisManager &FAM = | ||
| AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); | ||
| OffloadSanitizerImpl Impl(M, FAM); | ||
| if (!Impl.instrument()) | ||
| return PreservedAnalyses::all(); | ||
| LLVM_DEBUG(M.dump()); | ||
| return PreservedAnalyses::none(); | ||
| } | ||
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,79 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
|
|
||
| target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9" | ||
| target triple = "amdgcn-amd-amdhsa" | ||
|
|
||
| ; Test basic offload sanitizer trap instrumentation. | ||
|
|
||
| ; RUN: opt < %s -passes=offload-sanitizer -S | FileCheck --check-prefixes=CHECK %s | ||
|
|
||
| define void @test_trap1() { | ||
| ; CHECK-LABEL: define void @test_trap1() { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[PC:%.*]] = call i64 @llvm.amdgcn.s.getpc() | ||
| ; CHECK-NEXT: call void @__offload_san_trap_info(i64 [[PC]]) | ||
| ; CHECK-NEXT: call void @llvm.trap() | ||
| ; CHECK-NEXT: ret void | ||
| ; | ||
| entry: | ||
| call void @llvm.trap() | ||
| ret void | ||
| } | ||
|
|
||
| define void @test_trap2() { | ||
| ; CHECK-LABEL: define void @test_trap2() { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: [[PC:%.*]] = call i64 @llvm.amdgcn.s.getpc() | ||
| ; CHECK-NEXT: call void @__offload_san_trap_info(i64 [[PC]]) | ||
| ; CHECK-NEXT: call void @llvm.trap() | ||
| ; CHECK-NEXT: unreachable | ||
| ; | ||
| entry: | ||
| call void @llvm.trap() | ||
| unreachable | ||
| } | ||
|
|
||
| define void @test_trap3(i1 %c) { | ||
| ; CHECK-LABEL: define void @test_trap3( | ||
| ; CHECK-SAME: i1 [[C:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] | ||
| ; CHECK-NEXT: br i1 [[C]], label %[[T:.*]], label %[[F:.*]] | ||
| ; CHECK: [[T]]: | ||
| ; CHECK-NEXT: [[PC:%.*]] = call i64 @llvm.amdgcn.s.getpc() | ||
| ; CHECK-NEXT: call void @__offload_san_trap_info(i64 [[PC]]) | ||
| ; CHECK-NEXT: call void @llvm.trap() | ||
| ; CHECK-NEXT: unreachable | ||
| ; CHECK: [[F]]: | ||
| ; CHECK-NEXT: ret void | ||
| ; | ||
| entry: | ||
| br i1 %c, label %t, label %f | ||
| t: | ||
| call void @llvm.trap() | ||
| unreachable | ||
| f: | ||
| ret void | ||
| } | ||
jdoerfert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| define void @test_ubsantrap(i1 %c) { | ||
| ; CHECK-LABEL: define void @test_ubsantrap( | ||
| ; CHECK-SAME: i1 [[C:%.*]]) { | ||
| ; CHECK-NEXT: [[PC:%.*]] = call i64 @llvm.amdgcn.s.getpc() | ||
| ; CHECK-NEXT: call void @__offload_san_trap_info(i64 [[PC]]) | ||
| ; CHECK-NEXT: call void @llvm.ubsantrap(i8 42) | ||
| ; CHECK-NEXT: unreachable | ||
| ; | ||
| call void @llvm.ubsantrap(i8 42) | ||
| unreachable | ||
| } | ||
|
|
||
| define void @test_trap_no_san_attr(i1 %c) disable_sanitizer_instrumentation { | ||
| ; __attribute__((disable_sanitizer_instrumentation)) | ||
| ; CHECK-LABEL: define void @test_trap_no_san_attr( | ||
| ; CHECK-SAME: i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] { | ||
| ; CHECK-NEXT: call void @llvm.trap() | ||
| ; CHECK-NEXT: ret void | ||
| ; | ||
| call void @llvm.trap() | ||
| ret void | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.