Skip to content

Commit 31edeb9

Browse files
authored
Merge pull request #26 from RWTH-HPC/flang-tsan-poc
[Flang][WIP/RFC] Enable TSan for Flang
2 parents f8575ff + 5e0568c commit 31edeb9

File tree

7 files changed

+24
-6
lines changed

7 files changed

+24
-6
lines changed

clang/include/clang/Driver/Options.td

+4-4
Original file line numberDiff line numberDiff line change
@@ -1786,14 +1786,14 @@ def fmemory_profile_use_EQ : Joined<["-"], "fmemory-profile-use=">,
17861786
HelpText<"Use memory profile for profile-guided memory optimization">,
17871787
MarshallingInfoString<CodeGenOpts<"MemoryProfileUsePath">>;
17881788

1789+
def fsanitize_EQ : CommaJoined<["-"], "fsanitize=">, Group<f_clang_Group>,
1790+
MetaVarName<"<check>">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
1791+
HelpText<"Turn on runtime checks for various forms of undefined "
1792+
"or suspicious behavior. See user manual for available checks">;
17891793
// Begin sanitizer flags. These should all be core options exposed in all driver
17901794
// modes.
17911795
let Flags = [CC1Option, CoreOption] in {
17921796

1793-
def fsanitize_EQ : CommaJoined<["-"], "fsanitize=">, Group<f_clang_Group>,
1794-
MetaVarName<"<check>">,
1795-
HelpText<"Turn on runtime checks for various forms of undefined "
1796-
"or suspicious behavior. See user manual for available checks">;
17971797
def fno_sanitize_EQ : CommaJoined<["-"], "fno-sanitize=">, Group<f_clang_Group>,
17981798
Flags<[CoreOption, NoXarchOption]>;
17991799

clang/lib/Driver/ToolChains/Flang.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void Flang::addFortranDialectOptions(const ArgList &Args,
3636
options::OPT_fopenmp,
3737
options::OPT_fopenmp_version_EQ,
3838
options::OPT_fopenacc,
39+
options::OPT_fsanitize_EQ,
3940
options::OPT_finput_charset_EQ,
4041
options::OPT_fimplicit_none,
4142
options::OPT_fno_implicit_none,

flang/include/flang/Common/Fortran-features.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
3535
ProgramReturn, ImplicitNoneTypeNever, ImplicitNoneTypeAlways,
3636
ForwardRefImplicitNone, OpenAccessAppend, BOZAsDefaultInteger,
3737
DistinguishableSpecifics, DefaultSave, PointerInSeqType, NonCharacterFormat,
38-
SaveMainProgram, SaveBigMainProgramVariables,
38+
SaveMainProgram, SaveBigMainProgramVariables, TSan,
3939
DistinctArrayConstructorLengths, PPCVector, RelaxedIntentInChecking,
4040
ForwardRefImplicitNoneData)
4141

@@ -56,6 +56,7 @@ class LanguageFeatureControl {
5656
disable_.set(LanguageFeature::OldDebugLines);
5757
disable_.set(LanguageFeature::OpenACC);
5858
disable_.set(LanguageFeature::OpenMP);
59+
disable_.set(LanguageFeature::TSan);
5960
disable_.set(LanguageFeature::CUDA); // !@cuf
6061
disable_.set(LanguageFeature::ImplicitNoneTypeNever);
6162
disable_.set(LanguageFeature::ImplicitNoneTypeAlways);

flang/lib/Frontend/CompilerInvocation.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
726726
res.getFrontendOpts().features.Enable(
727727
Fortran::common::LanguageFeature::OpenACC);
728728
}
729+
if (args.hasArg(clang::driver::options::OPT_fsanitize_EQ) && llvm::StringRef(args.getLastArg(clang::driver::options::OPT_fsanitize_EQ)->getValue()) == "thread" ) {
730+
res.getFrontendOpts().features.Enable(
731+
Fortran::common::LanguageFeature::TSan);
732+
}
729733
if (args.hasArg(clang::driver::options::OPT_fopenmp)) {
730734
// By default OpenMP is set to 1.1 version
731735
res.getLangOpts().OpenMPVersion = 11;

flang/lib/Frontend/FrontendActions.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
#include "llvm/Target/TargetMachine.h"
6464
#include "llvm/TargetParser/TargetParser.h"
6565
#include "llvm/Transforms/Utils/ModuleUtils.h"
66+
#include "llvm/Transforms/Instrumentation.h"
67+
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
68+
6669
#include <memory>
6770
#include <system_error>
6871

@@ -913,6 +916,11 @@ void CodeGenAction::runOptimizationPipeline(llvm::raw_pwrite_stream &os) {
913916
else
914917
mpm = pb.buildPerModuleDefaultPipeline(level);
915918

919+
if (this->getInstance().getInvocation().getFrontendOpts().features.IsEnabled(
920+
Fortran::common::LanguageFeature::TSan)) {
921+
mpm.addPass(llvm::ModuleThreadSanitizerPass());
922+
mpm.addPass(llvm::createModuleToFunctionPassAdaptor(llvm::ThreadSanitizerPass()));
923+
}
916924
if (action == BackendActionTy::Backend_EmitBC)
917925
mpm.addPass(llvm::BitcodeWriterPass(os));
918926

flang/lib/Optimizer/CodeGen/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
set(LLVM_LINK_COMPONENTS
2+
Instrumentation
3+
)
4+
15
add_flang_library(FIRCodeGen
26
BoxedProcedure.cpp
37
CGOps.cpp

llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ bool ThreadSanitizer::sanitizeFunction(Function &F,
516516
SmallVector<Instruction*, 8> MemIntrinCalls;
517517
bool Res = false;
518518
bool HasCalls = false;
519-
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeThread);
519+
bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeThread) || true;
520520
const DataLayout &DL = F.getParent()->getDataLayout();
521521

522522
// Traverse all instructions, collect loads/stores/returns, check for calls.

0 commit comments

Comments
 (0)