Skip to content

Commit d6c87d8

Browse files
Changes by gamba
1 parent c713eef commit d6c87d8

File tree

2 files changed

+70
-47
lines changed

2 files changed

+70
-47
lines changed

passes/ASPIS.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ class RACFED : public PassInfoMixin<RACFED> {
160160
private:
161161
std::map<Value *, StringRef> FuncAnnotations;
162162
std::map<BasicBlock *, BasicBlock *> NewBBs;
163-
std::unordered_map<BasicBlock *, int> compileTimeSig;
164-
std::unordered_map<BasicBlock *, int> subRanPrevVals;
165-
std::unordered_map<BasicBlock *, int> &sumIntraInstruction;
163+
std::unordered_map<BasicBlock *, int> compileTimeSig;
164+
std::unordered_map<BasicBlock *, int> subRanPrevVals;
165+
std::unordered_map<BasicBlock *, int> sumIntraInstruction;
166166

167167

168168
#if (LOG_COMPILED_FUNCS == 1)
@@ -171,15 +171,11 @@ class RACFED : public PassInfoMixin<RACFED> {
171171

172172
// -- INITIALIZE BLOCKS --
173173
void initializeBlocksSignatures(Module &Md);
174-
bool isNotUniqueCompileTimeSig(int bb_num);
175174
// -- UPDATE COMPILE SIG RANDOM --
176-
void updateCompileSigRandom();
175+
void updateCompileSigRandom(Function &F, Module &Md);
177176

178177
// -- CREATE CFG VERIFICATION --
179178

180-
181-
void originalInstruction(BasicBlock &BB,std::vector<Instruction*> OrigInstructions);
182-
183179
void splitBBsAtCalls(Module &Md);
184180
int countOriginalInstructions(BasicBlock &BB);
185181
CallBase *isCallBB(BasicBlock &BB);

passes/RACFED.cpp

Lines changed: 66 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// the code
1313
#define INTRA_FUNCTION_CFC 0 // Default to 0 if not defined
1414

15+
#define MARTI_DEBUG true
16+
1517
using namespace llvm;
1618

1719
/**
@@ -53,7 +55,9 @@ using namespace llvm;
5355

5456
// --- INITIALIZE BLOCKS RANDOM ---
5557

56-
bool RACFED::isNotUniqueCompileTimeSig(int bb_num) {
58+
bool isNotUniqueCompileTimeSig(int bb_num,
59+
const std::unordered_map<BasicBlock*, int> &compileTimeSig
60+
) {
5761
for (const auto &pair : compileTimeSig) {
5862
if (pair.second == bb_num) return true;
5963
}
@@ -86,7 +90,7 @@ void RACFED::initializeBlocksSignatures(Module &Md) {
8690
for (BasicBlock &BB : Fn) {
8791
do {
8892
randomBB = rand();
89-
} while (isNotUniqueCompileTimeSig(randomBB));
93+
} while (isNotUniqueCompileTimeSig(randomBB, compileTimeSig));
9094

9195
do {
9296
randomSub = rand();
@@ -102,6 +106,26 @@ void RACFED::initializeBlocksSignatures(Module &Md) {
102106

103107

104108
// --- UPDATE SIGNATURE RANDOM ---
109+
void originalInstruction(BasicBlock &BB, std::vector<Instruction*> &OrigInstructions) {
110+
111+
for (Instruction &I : BB) {
112+
if (isa<PHINode>(&I)) continue; // NON è originale
113+
if (I.isTerminator()) continue; // NON è originale
114+
if (isa<DbgInfoIntrinsic>(&I)) continue; // debug, ignora OrigInstructions.push_back(&I);
115+
OrigInstructions.push_back(&I);
116+
}
117+
}
118+
119+
int countOriginalInstructions(BasicBlock &BB) {
120+
int count = 0;
121+
for (Instruction &I : BB) {
122+
if (isa<PHINode>(&I)) continue; // NON è originale
123+
if (I.isTerminator()) continue; // NON è originale
124+
if (isa<DbgInfoIntrinsic>(&I)) continue; // debug, ignora
125+
count++;
126+
}
127+
return count;
128+
}
105129

106130
void RACFED::updateCompileSigRandom(Function &F, Module &Md) {
107131
LLVMContext &Ctx = Md.getContext();
@@ -110,6 +134,9 @@ void RACFED::updateCompileSigRandom(Function &F, Module &Md) {
110134
std::uniform_int_distribution<uint32_t> dist(1, 0x7fffffff);
111135
auto *I32 = Type::getInt32Ty(Ctx);
112136
if (!SigGV) {
137+
#if MARTI_DEBUG
138+
errs() << "SigGV " << SigGV << "\n";
139+
#endif
113140
SigGV = new GlobalVariable(
114141
Md,
115142
I32,
@@ -160,8 +187,6 @@ void RACFED::updateCompileSigRandom(Function &F, Module &Md) {
160187
// --- CREATE CFG VERIFICATION ---
161188

162189

163-
164-
165190
void RACFED::splitBBsAtCalls(Module &Md) {
166191
for (Function &Fn : Md) {
167192
if (shouldCompile(Fn, FuncAnnotations)) {
@@ -326,51 +351,53 @@ void RACFED::createCFGVerificationBB(
326351
}
327352

328353
PreservedAnalyses RACFED::run(Module &Md, ModuleAnalysisManager &AM) {
329-
330354
// mappa: istruzione originale -> random r usato per S = S + r
331355
std::unordered_map<llvm::Instruction*, int> InstrUpdates;
332356
auto *IntType = llvm::Type::getInt32Ty(Md.getContext());
333357
std::unordered_map<Function*, BasicBlock*> ErrBBs;
334358

335-
createFtFuncs(Md);
359+
// createFtFuncs(Md);
336360
getFuncAnnotations(Md, FuncAnnotations);
337-
LinkageMap linkageMap = mapFunctionLinkageNames((Md));
361+
LinkageMap linkageMap = mapFunctionLinkageNames((Md));
338362

339363
initializeBlocksSignatures(Md);
340364

341-
updateCompileSigRandom();
342-
343-
createCFGVerificationBB();
344-
345-
for (Function &Fn : Md) {
346-
if (!shouldCompile(Fn, FuncAnnotations))
347-
continue;
348-
349-
if (shouldCompile(Fn, FuncAnnotations)) {
350-
DebugLoc debugLoc;
351-
for (auto &I : Fn.front()) {
352-
if (I.getDebugLoc()) {
353-
debugLoc = I.getDebugLoc();
354-
break;
355-
}
356-
}
357-
358-
for (BasicBlock &BB : Fn) {
359-
std::vector<Instruction*> OrigInstructions;
360-
originalInstruction(BB, OrigInstructions);
361-
if (OrigInstructions.size()<2)
362-
continue;
363-
int sumOfIntraUpdates = 0;
364-
auto *IntType = llvm::Type::getInt32Ty(BB.getContext()); // control to get context for add and compare ...
365-
366-
for (Instruction *I:OrigInstructions) {
367-
int r = rand();
368-
sumOfIntraUpdates += r;
369-
InstrUpdates[I]=r;
370-
}
371-
sumIntraInstruction[&BB] = sumOfIntraUpdates;
372-
}
365+
for (Function &F: Md){
366+
updateCompileSigRandom(F, Md);
373367
}
368+
369+
// createCFGVerificationBB();
370+
//
371+
// for (Function &Fn : Md) {
372+
// if (!shouldCompile(Fn, FuncAnnotations))
373+
// continue;
374+
//
375+
// if (shouldCompile(Fn, FuncAnnotations)) {
376+
// DebugLoc debugLoc;
377+
// for (auto &I : Fn.front()) {
378+
// if (I.getDebugLoc()) {
379+
// debugLoc = I.getDebugLoc();
380+
// break;
381+
// }
382+
// }
383+
//
384+
// for (BasicBlock &BB : Fn) {
385+
// std::vector<Instruction*> OrigInstructions;
386+
// originalInstruction(BB, OrigInstructions);
387+
// if (OrigInstructions.size()<2)
388+
// continue;
389+
// int sumOfIntraUpdates = 0;
390+
// auto *IntType = llvm::Type::getInt32Ty(BB.getContext()); // control to get context for add and compare ...
391+
//
392+
// for (Instruction *I:OrigInstructions) {
393+
// int r = rand();
394+
// sumOfIntraUpdates += r;
395+
// InstrUpdates[I]=r;
396+
// }
397+
// sumIntraInstruction[&BB] = sumOfIntraUpdates;
398+
// }
399+
// }
400+
return PreservedAnalyses::all();
374401
}
375402

376403

0 commit comments

Comments
 (0)