@@ -33,6 +33,7 @@ limitations under the License.
33
33
#include " llvm/Pass.h"
34
34
#include " llvm/PassRegistry.h"
35
35
#include " llvm/Passes/PassBuilder.h"
36
+ #include " llvm/Passes/PassPlugin.h"
36
37
#include " llvm/Support/CommandLine.h"
37
38
#include " llvm/Support/Error.h"
38
39
#include " llvm/Support/FileSystem.h"
@@ -108,11 +109,25 @@ static void addFuzzerInstrumentationPass(llvm::ModulePassManager &MPM)
108
109
MPM.addPass (SanitizerCoveragePass (opts));
109
110
}
110
111
112
+ class AddWindowsDLLExportPass : public PassInfoMixin <AddWindowsDLLExportPass>
113
+ {
114
+ public:
115
+ PreservedAnalyses run (Function &F, FunctionAnalysisManager &AM)
116
+ {
117
+ if (F.getName ().starts_with (" rl__" ) or F.getName ().starts_with (" rl_m__" ))
118
+ return PreservedAnalyses::none ();
119
+
120
+ F.setDLLStorageClass (llvm::GlobalValue::DLLExportStorageClass);
121
+ return PreservedAnalyses::none ();
122
+ };
123
+ };
124
+
111
125
static void runOptimizer (
112
126
llvm::Module &M,
113
127
bool optimize,
114
128
bool emitSanitizerInstrumentation,
115
- bool linkAgainsFuzzer)
129
+ bool linkAgainsFuzzer,
130
+ bool targetIsWindows)
116
131
{
117
132
// Create the analysis managers.
118
133
LoopAnalysisManager LAM;
@@ -139,10 +154,12 @@ static void runOptimizer(
139
154
{
140
155
ModulePassManager passManager;
141
156
FunctionPassManager functionPassManager;
157
+ if (targetIsWindows)
158
+ functionPassManager.addPass (AddWindowsDLLExportPass ());
142
159
functionPassManager.addPass (llvm::PromotePass ());
143
160
passManager.addPass (
144
161
createModuleToFunctionPassAdaptor (std::move (functionPassManager)));
145
- if (emitSanitizerInstrumentation)
162
+ if (emitSanitizerInstrumentation and not targetIsWindows )
146
163
addFuzzerInstrumentationPass (passManager);
147
164
passManager.run (M, MAM);
148
165
@@ -163,7 +180,9 @@ static void runOptimizer(
163
180
{
164
181
ModulePassManager MPM =
165
182
PB.buildO0DefaultPipeline (OptimizationLevel::O0, true );
166
- if (emitSanitizerInstrumentation)
183
+ if (targetIsWindows)
184
+ MPM.addPass (createModuleToFunctionPassAdaptor (AddWindowsDLLExportPass ()));
185
+ if (emitSanitizerInstrumentation and not targetIsWindows)
167
186
addFuzzerInstrumentationPass (MPM);
168
187
MPM.run (M, MAM);
169
188
}
@@ -380,22 +399,37 @@ static int linkLibraries(
380
399
argSource.push_back (" -fuse-ld=lld" );
381
400
argSource.push_back (" -Wl,-subsystem:console" );
382
401
}
383
- argSource.push_back (" -o" );
384
- argSource.push_back (outputFile.str ());
385
- if (not info.isMacOS ())
402
+ else if (not info.isMacOS ())
386
403
argSource.push_back (" -lm" );
404
+
405
+ argSource.push_back (" -o" );
387
406
if (shared)
388
407
{
389
- argSource.push_back (" --shared" );
390
- if (not info.isWindows ())
408
+ if (info.isWindows ())
409
+ {
410
+ argSource.push_back (outputFile.str () + " .dll" );
411
+ }
412
+ else
413
+ {
414
+ argSource.push_back (outputFile.str ());
391
415
argSource.push_back (" -fPIE" );
416
+ }
417
+ argSource.push_back (" --shared" );
392
418
}
393
419
else
394
420
{
395
- if (not info.isWindows ())
421
+ if (info.isWindows ())
422
+ {
423
+ argSource.push_back (outputFile.str () + " .exe" );
424
+ }
425
+ else
426
+ {
427
+ argSource.push_back (outputFile.str ());
396
428
argSource.push_back (" -no-pie" );
429
+ }
397
430
}
398
- if (emitSanitizerInstrumentation or linkAgainstFuzzer)
431
+ if ((emitSanitizerInstrumentation or linkAgainstFuzzer) and
432
+ not info.isWindows ())
399
433
{
400
434
std::string arg (" -fsanitize=" );
401
435
if (emitSanitizerInstrumentation)
@@ -405,11 +439,13 @@ static int linkLibraries(
405
439
if (linkAgainstFuzzer)
406
440
arg += " fuzzer" ;
407
441
argSource.push_back (arg);
442
+ if (info.isWindows ())
443
+ {
444
+ argSource.push_back (" -Wl,/NODEFAULTLIB:libcmt" );
445
+ argSource.push_back (" -Wl,-defaultlib:msvcrt" );
446
+ }
408
447
}
409
448
410
- for (auto rpathEntry : rpaths)
411
- argSource.push_back (" -Wl,-rpath," + rpathEntry);
412
-
413
449
for (auto extraObject : extraObjectFiles)
414
450
argSource.push_back (extraObject);
415
451
@@ -441,7 +477,12 @@ namespace mlir::rlc
441
477
assert (Module);
442
478
Module->setTargetTriple (llvm::sys::getDefaultTargetTriple ());
443
479
444
- runOptimizer (*Module, targetInfo->optimize (), emitSanitizer, emitFuzzer);
480
+ runOptimizer (
481
+ *Module,
482
+ targetInfo->optimize (),
483
+ emitSanitizer,
484
+ emitFuzzer,
485
+ targetInfo->isWindows ());
445
486
446
487
if (dumpIR)
447
488
{
0 commit comments