Skip to content

Commit 2521a3b

Browse files
committed
[WIP] Playing with ideas
1 parent 937a987 commit 2521a3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2980
-334
lines changed

clang/include/clang/Basic/Sanitizers.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ SANITIZER_GROUP("bounds", Bounds, ArrayBounds | LocalBounds)
190190
// Scudo hardened allocator
191191
SANITIZER("scudo", Scudo)
192192

193+
// LLVM/Offload sanitizer
194+
SANITIZER("offload", Offload)
195+
193196
// Magic group, containing all sanitizers. For example, "-fno-sanitize=all"
194197
// can be used to disable all the sanitizers.
195198
SANITIZER_GROUP("all", All, ~SanitizerMask())

clang/include/clang/Driver/SanitizerArgs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class SanitizerArgs {
8080

8181
bool needsMemProfRt() const { return NeedsMemProfRt; }
8282
bool needsAsanRt() const { return Sanitizers.has(SanitizerKind::Address); }
83+
bool needsOffloadKernels() const {
84+
return Sanitizers.has(SanitizerKind::Offload);
85+
}
8386
bool needsHwasanRt() const {
8487
return Sanitizers.has(SanitizerKind::HWAddress);
8588
}

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
7171
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
7272
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
73+
#include "llvm/Transforms/Instrumentation/GPUSan.h"
7374
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
7475
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7576
#include "llvm/Transforms/Instrumentation/KCFI.h"

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ llvm::Function *CodeGenModule::CreateGlobalInitOrCleanUpFunction(
463463
!isInNoSanitizeList(SanitizerKind::Address, Fn, Loc))
464464
Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
465465

466+
if (getLangOpts().Sanitize.has(SanitizerKind::Offload) &&
467+
!isInNoSanitizeList(SanitizerKind::Offload, Fn, Loc))
468+
Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
469+
466470
if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
467471
!isInNoSanitizeList(SanitizerKind::KernelAddress, Fn, Loc))
468472
Fn->addFnAttr(llvm::Attribute::SanitizeAddress);

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/AST/StmtObjC.h"
3232
#include "clang/Basic/Builtins.h"
3333
#include "clang/Basic/CodeGenOptions.h"
34+
#include "clang/Basic/Sanitizers.h"
3435
#include "clang/Basic/TargetBuiltins.h"
3536
#include "clang/Basic/TargetInfo.h"
3637
#include "clang/CodeGen/CGFunctionInfo.h"
@@ -67,7 +68,8 @@ static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts,
6768
// Sanitizers may use markers.
6869
if (CGOpts.SanitizeAddressUseAfterScope ||
6970
LangOpts.Sanitize.has(SanitizerKind::HWAddress) ||
70-
LangOpts.Sanitize.has(SanitizerKind::Memory))
71+
LangOpts.Sanitize.has(SanitizerKind::Memory) ||
72+
LangOpts.Sanitize.has(SanitizerKind::Offload))
7173
return true;
7274

7375
// For now, only in optimized builds.
@@ -808,6 +810,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
808810
SanOpts.set(SanitizerKind::KernelHWAddress, false);
809811
if (no_sanitize_mask & SanitizerKind::KernelHWAddress)
810812
SanOpts.set(SanitizerKind::HWAddress, false);
813+
if (no_sanitize_mask & SanitizerKind::Offload)
814+
SanOpts.set(SanitizerKind::Offload, false);
811815

812816
if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
813817
Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
@@ -826,7 +830,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
826830
CurFn->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
827831
} else {
828832
// Apply sanitizer attributes to the function.
829-
if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
833+
if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress |
834+
SanitizerKind::Offload))
830835
Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
831836
if (SanOpts.hasOneOf(SanitizerKind::HWAddress |
832837
SanitizerKind::KernelHWAddress))

clang/lib/Driver/ToolChain.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,8 @@ SanitizerMask ToolChain::getSupportedSanitizers() const {
13881388
SanitizerKind::CFICastStrict | SanitizerKind::FloatDivideByZero |
13891389
SanitizerKind::KCFI | SanitizerKind::UnsignedIntegerOverflow |
13901390
SanitizerKind::UnsignedShiftBase | SanitizerKind::ImplicitConversion |
1391-
SanitizerKind::Nullability | SanitizerKind::LocalBounds;
1391+
SanitizerKind::Nullability | SanitizerKind::LocalBounds |
1392+
SanitizerKind::Offload;
13921393
if (getTriple().getArch() == llvm::Triple::x86 ||
13931394
getTriple().getArch() == llvm::Triple::x86_64 ||
13941395
getTriple().getArch() == llvm::Triple::arm ||

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,12 @@ bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
11981198
const ToolChain &TC, const ArgList &Args,
11991199
bool ForceStaticHostRuntime, bool IsOffloadingHost,
12001200
bool GompNeedsRT) {
1201+
const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args);
1202+
if (SanArgs.needsOffloadKernels()) {
1203+
CmdArgs.push_back("-loffload.kernels");
1204+
CmdArgs.append({"-mllvm", "-enable-offload-sanitizer"});
1205+
}
1206+
12011207
if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
12021208
options::OPT_fno_openmp, false)) {
12031209
// We need libomptarget (liboffload) if it's the choosen offloading runtime.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- Transforms/Instrumentation/GPUSan.h ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
/// This file provides the interface for LLVM's PGO Instrumentation lowering
10+
/// pass.
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_GPUSAN_H
14+
#define LLVM_TRANSFORMS_INSTRUMENTATION_GPUSAN_H
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
class GPUSanPass : public PassInfoMixin<GPUSanPass> {
21+
public:
22+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
23+
};
24+
} // end namespace llvm
25+
26+
#endif // LLVM_TRANSFORMS_INSTRUMENTATION_GPUSAN_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
189189
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
190190
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
191+
#include "llvm/Transforms/Instrumentation/GPUSan.h"
191192
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
192193
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
193194
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "llvm/Transforms/InstCombine/InstCombine.h"
7373
#include "llvm/Transforms/Instrumentation/CGProfile.h"
7474
#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
75+
#include "llvm/Transforms/Instrumentation/GPUSan.h"
7576
#include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
7677
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
7778
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
@@ -164,6 +165,10 @@ static cl::opt<bool>
164165
cl::Hidden,
165166
cl::desc("Enable inline deferral during PGO"));
166167

168+
static cl::opt<bool>
169+
EnableOffloadSanitizer("enable-offload-sanitizer", cl::init(false),
170+
cl::Hidden, cl::desc("Enable offload sanitizer"));
171+
167172
static cl::opt<bool> EnableModuleInliner("enable-module-inliner",
168173
cl::init(false), cl::Hidden,
169174
cl::desc("Enable module inliner"));
@@ -1782,6 +1787,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
17821787
// in ICP.
17831788
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
17841789

1790+
if (EnableOffloadSanitizer)
1791+
MPM.addPass(GPUSanPass());
1792+
17851793
invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
17861794

17871795
// Emit annotation remarks.
@@ -1860,6 +1868,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
18601868
// pipeline).
18611869
MPM.addPass(LowerTypeTestsPass(nullptr, nullptr, true));
18621870

1871+
if (EnableOffloadSanitizer)
1872+
MPM.addPass(GPUSanPass());
1873+
18631874
invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
18641875

18651876
// Emit annotation remarks.
@@ -2076,6 +2087,9 @@ PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
20762087
if (PTO.CallGraphProfile)
20772088
MPM.addPass(CGProfilePass(/*InLTOPostLink=*/true));
20782089

2090+
if (EnableOffloadSanitizer)
2091+
MPM.addPass(GPUSanPass());
2092+
20792093
invokeFullLinkTimeOptimizationLastEPCallbacks(MPM, Level);
20802094

20812095
// Emit annotation remarks.

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ MODULE_PASS("inliner-wrapper-no-mandatory-first",
8080
MODULE_PASS("insert-gcov-profiling", GCOVProfilerPass())
8181
MODULE_PASS("instrorderfile", InstrOrderFilePass())
8282
MODULE_PASS("instrprof", InstrProfilingLoweringPass())
83+
MODULE_PASS("gpusan", GPUSanPass())
8384
MODULE_PASS("ctx-instr-lower", PGOCtxProfLoweringPass())
8485
MODULE_PASS("print<ctx-prof-analysis>", CtxProfAnalysisPrinterPass(dbgs()))
8586
MODULE_PASS("invalidate<all>", InvalidateAllAnalysesPass())

llvm/lib/Transforms/IPO/AttributorAttributes.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,7 +7539,7 @@ struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
75397539
// Collect all tail calls in the function as we cannot allow new allocas to
75407540
// escape into tail recursion.
75417541
// TODO: Be smarter about new allocas escaping into tail calls.
7542-
SmallVector<CallInst *, 16> TailCalls;
7542+
SmallVector<WeakTrackingVH, 16> TailCalls;
75437543
bool UsedAssumedInformation = false;
75447544
if (!A.checkForAllInstructions(
75457545
[&](Instruction &I) {
@@ -7577,8 +7577,9 @@ struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
75777577
AI, Arg->getType(), "", IP);
75787578
Arg->replaceAllUsesWith(AI);
75797579

7580-
for (CallInst *CI : TailCalls)
7581-
CI->setTailCall(false);
7580+
for (auto &CI : TailCalls)
7581+
if (CI)
7582+
cast<CallInst>(CI)->setTailCall(false);
75827583
};
75837584

75847585
// Callback to repair a call site of the associated function. The elements

llvm/lib/Transforms/IPO/OpenMPOpt.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,13 @@ struct OMPInformationCache : public InformationCache {
619619
// functions, except if `optnone` is present.
620620
if (isOpenMPDevice(M)) {
621621
for (Function &F : M) {
622-
for (StringRef Prefix : {"__kmpc", "_ZN4ompx", "omp_"})
623-
if (F.hasFnAttribute(Attribute::NoInline) &&
624-
F.getName().starts_with(Prefix) &&
625-
!F.hasFnAttribute(Attribute::OptimizeNone))
626-
F.removeFnAttr(Attribute::NoInline);
622+
for (StringRef Prefix : {"__kmpc", "_ZN4ompx", "omp_"}) {
623+
if (!F.getName().starts_with(Prefix) ||
624+
F.hasFnAttribute(Attribute::OptimizeNone))
625+
continue;
626+
F.removeFnAttr(Attribute::NoInline);
627+
F.addFnAttr(Attribute::AlwaysInline);
628+
}
627629
}
628630
}
629631

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ add_llvm_component_library(LLVMInstrumentation
66
DataFlowSanitizer.cpp
77
GCOVProfiling.cpp
88
BlockCoverageInference.cpp
9+
GPUSan.cpp
910
MemProfiler.cpp
1011
MemorySanitizer.cpp
1112
NumericalStabilitySanitizer.cpp

0 commit comments

Comments
 (0)