Skip to content

Commit 762db7d

Browse files
Add numerical sanitizer
1 parent d4f5cf2 commit 762db7d

File tree

14 files changed

+3909
-0
lines changed

14 files changed

+3909
-0
lines changed

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ enum AttributeKindCodes {
744744
ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE = 90,
745745
ATTR_KIND_DEAD_ON_UNWIND = 91,
746746
ATTR_KIND_RANGE = 92,
747+
ATTR_KIND_SANITIZE_NUMERICAL_STABILITY = 93,
747748
};
748749

749750
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ def SanitizeHWAddress : EnumAttr<"sanitize_hwaddress", [FnAttr]>;
285285
/// MemTagSanitizer is on.
286286
def SanitizeMemTag : EnumAttr<"sanitize_memtag", [FnAttr]>;
287287

288+
/// NumericalStabilitySanitizer is on.
289+
def SanitizeNumericalStability : EnumAttr<"sanitize_numericalstability", [FnAttr]>;
290+
288291
/// Speculative Load Hardening is enabled.
289292
///
290293
/// Note that this uses the default compatibility (always compatible during
@@ -372,6 +375,7 @@ def : CompatRule<"isEqual<SanitizeThreadAttr>">;
372375
def : CompatRule<"isEqual<SanitizeMemoryAttr>">;
373376
def : CompatRule<"isEqual<SanitizeHWAddressAttr>">;
374377
def : CompatRule<"isEqual<SanitizeMemTagAttr>">;
378+
def : CompatRule<"isEqual<SanitizeNumericalStabilityAttr>">;
375379
def : CompatRule<"isEqual<SafeStackAttr>">;
376380
def : CompatRule<"isEqual<ShadowCallStackAttr>">;
377381
def : CompatRule<"isEqual<UseSampleProfileAttr>">;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- NumericalStabilitySanitizer.h - NSan Pass ---------------*- 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+
///
9+
/// \file
10+
/// This file defines the numerical stability sanitizer (nsan) pass.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_NUMERICALSTABIITYSANITIZER_H
15+
#define LLVM_TRANSFORMS_INSTRUMENTATION_NUMERICALSTABIITYSANITIZER_H
16+
17+
#include "llvm/IR/PassManager.h"
18+
#include "llvm/Pass.h"
19+
20+
namespace llvm {
21+
22+
/// Inserts NumericalStabilitySanitizer instrumentation.
23+
// FunctionPass *createNumericalStabilitySanitizerLegacyPassPass();
24+
25+
/// A function pass for nsan instrumentation.
26+
///
27+
/// Instruments functions to duplicate floating point computations in a
28+
/// higher-precision type.
29+
/// This function pass inserts calls to runtime library functions. If the
30+
/// functions aren't declared yet, the pass inserts the declarations.
31+
struct NumericalStabilitySanitizerPass
32+
: public PassInfoMixin<NumericalStabilitySanitizerPass> {
33+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
34+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
35+
static bool isRequired() { return true; }
36+
};
37+
38+
} // end namespace llvm
39+
40+
#endif // LLVM_TRANSFORMS_INSTRUMENTATION_NUMERICALSTABIITYSANITIZER_H

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
21062106
return Attribute::SanitizeThread;
21072107
case bitc::ATTR_KIND_SANITIZE_MEMORY:
21082108
return Attribute::SanitizeMemory;
2109+
case bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY:
2110+
return Attribute::SanitizeNumericalStability;
21092111
case bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING:
21102112
return Attribute::SpeculativeLoadHardening;
21112113
case bitc::ATTR_KIND_SWIFT_ERROR:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
819819
return bitc::ATTR_KIND_SANITIZE_THREAD;
820820
case Attribute::SanitizeMemory:
821821
return bitc::ATTR_KIND_SANITIZE_MEMORY;
822+
case Attribute::SanitizeNumericalStability:
823+
return bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY;
822824
case Attribute::SpeculativeLoadHardening:
823825
return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
824826
case Attribute::SwiftError:

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
#include "llvm/Transforms/Instrumentation/LowerAllowCheckPass.h"
178178
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
179179
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
180+
#include "llvm/Transforms/Instrumentation/NumericalStabilitySanitizer.h"
180181
#include "llvm/Transforms/Instrumentation/PGOCtxProfLowering.h"
181182
#include "llvm/Transforms/Instrumentation/PGOForceFunctionAttrs.h"
182183
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ MODULE_PASS("metarenamer", MetaRenamerPass())
9494
MODULE_PASS("module-inline", ModuleInlinerPass())
9595
MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
9696
MODULE_PASS("no-op-module", NoOpModulePass())
97+
MODULE_PASS("nsan-module", NumericalStabilitySanitizerPass())
9798
MODULE_PASS("objc-arc-apelim", ObjCARCAPElimPass())
9899
MODULE_PASS("openmp-opt", OpenMPOptPass())
99100
MODULE_PASS("openmp-opt-postlink",
@@ -386,6 +387,7 @@ FUNCTION_PASS("move-auto-init", MoveAutoInitPass())
386387
FUNCTION_PASS("nary-reassociate", NaryReassociatePass())
387388
FUNCTION_PASS("newgvn", NewGVNPass())
388389
FUNCTION_PASS("no-op-function", NoOpFunctionPass())
390+
FUNCTION_PASS("nsan", NumericalStabilitySanitizerPass())
389391
FUNCTION_PASS("objc-arc", ObjCARCOptPass())
390392
FUNCTION_PASS("objc-arc-contract", ObjCARCContractPass())
391393
FUNCTION_PASS("objc-arc-expand", ObjCARCExpandPass())

llvm/lib/Transforms/Instrumentation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_llvm_component_library(LLVMInstrumentation
88
BlockCoverageInference.cpp
99
MemProfiler.cpp
1010
MemorySanitizer.cpp
11+
NumericalStabilitySanitizer.cpp
1112
IndirectCallPromotion.cpp
1213
Instrumentation.cpp
1314
InstrOrderFile.cpp

0 commit comments

Comments
 (0)