Skip to content

Commit 0309ccb

Browse files
Misc fixes (#615)
* Fix some bugs * Add utils to LLVMIRToSrc * Make the Container type parameter of the flow function templates actually settable * Fix recursive template instantiation in C++20 mode * feat: boolean to toggle trim --------- Co-authored-by: Sriteja Kummita <sriteja.ku@gmail.com>
1 parent 88e42a2 commit 0309ccb

File tree

6 files changed

+86
-62
lines changed

6 files changed

+86
-62
lines changed

include/phasar/DB/ProjectIRDBBase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ template <typename Derived> class ProjectIRDBBase {
112112
/// module for this function to work
113113
[[nodiscard]] size_t getInstructionId(n_t Inst) const {
114114
assert(isValid());
115-
return self().getInstructionId(Inst);
115+
return self().getInstructionIdImpl(Inst);
116116
}
117117

118118
[[nodiscard]] decltype(auto) getAllInstructions() const {

include/phasar/DataFlow/IfdsIde/EdgeFunction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ template <typename T>
5858
concept IsEdgeFunction = requires(const T &EF, const EdgeFunction<typename T::l_t>& TEEF, EdgeFunctionRef<T> CEF, typename T::l_t Src) {
5959
typename T::l_t;
6060
{EF.computeTarget(Src)} -> std::convertible_to<typename T::l_t>;
61-
{T::compose(CEF, TEEF)} -> std::convertible_to<EdgeFunction<typename T::l_t>>;
62-
{T::join(CEF, TEEF)} -> std::convertible_to<EdgeFunction<typename T::l_t>>;
61+
{T::compose(CEF, TEEF)} -> std::same_as<EdgeFunction<typename T::l_t>>;
62+
{T::join(CEF, TEEF)} -> std::same_as<EdgeFunction<typename T::l_t>>;
6363
};
6464
// clang-format on
6565

include/phasar/DataFlow/IfdsIde/FlowFunctions.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/ArrayRef.h"
2323

2424
#include <functional>
25+
#include <initializer_list>
2526
#include <iterator>
2627
#include <memory>
2728
#include <set>
@@ -133,11 +134,8 @@ template <typename D, typename Container = std::set<D>> auto identityFlow() {
133134
/// v v v v v
134135
/// x1 x2 x x3 x4
135136
///
136-
template <typename D, typename Fn, typename Container = std::set<D>,
137-
typename = std::enable_if_t<
138-
std::is_invocable_v<Fn, D> &&
139-
std::is_convertible_v<std::invoke_result_t<Fn, D>, Container>>>
140-
auto lambdaFlow(Fn &&F) {
137+
template <typename D, typename Fn> auto lambdaFlow(Fn &&F) {
138+
using Container = std::invoke_result_t<Fn, D>;
141139
struct LambdaFlow final : public FlowFunction<D, Container> {
142140
LambdaFlow(Fn &&F) : Flow(std::forward<Fn>(F)) {}
143141
Container computeTargets(D Source) override {
@@ -204,7 +202,8 @@ auto generateFlow(psr::type_identity_t<D> FactToGenerate, D From) {
204202
/// f(x) = {v, x} if p(x) == true
205203
/// f(x) = {x} else.
206204
///
207-
template <typename D, typename Fn, typename Container = std::set<D>,
205+
template <typename D, typename Container = std::set<D>,
206+
typename Fn = psr::TrueFn,
208207
typename = std::enable_if_t<std::is_invocable_r_v<bool, Fn, D>>>
209208
auto generateFlowIf(D FactToGenerate, Fn Predicate) {
210209
struct GenFlowIf final : public FlowFunction<D, Container> {
@@ -243,7 +242,8 @@ auto generateFlowIf(D FactToGenerate, Fn Predicate) {
243242
/// v v v v ... \ v ...
244243
/// x w v1 v2 ... vN u
245244
///
246-
template <typename D, typename Range, typename Container = std::set<D>,
245+
template <typename D, typename Container = std::set<D>,
246+
typename Range = std::initializer_list<D>,
247247
typename = std::enable_if_t<is_iterable_over_v<Range, D>>>
248248
auto generateManyFlows(Range &&FactsToGenerate, D From) {
249249
struct GenMany final : public FlowFunction<D, Container> {
@@ -319,7 +319,8 @@ auto killFlow(D FactToKill) {
319319
/// f(x) = {} if p(x) == true
320320
/// f(x) = {x} else.
321321
///
322-
template <typename D, typename Fn, typename Container = std::set<D>,
322+
template <typename D, typename Container = std::set<D>,
323+
typename Fn = psr::TrueFn,
323324
typename = std::enable_if_t<std::is_invocable_r_v<bool, Fn, D>>>
324325
auto killFlowIf(Fn Predicate) {
325326
struct KillFlowIf final : public FlowFunction<D, Container> {
@@ -357,7 +358,8 @@ auto killFlowIf(Fn Predicate) {
357358
/// v v
358359
/// u v1 v2 ... vN w ...
359360
///
360-
template <typename D, typename Range, typename Container = std::set<D>,
361+
template <typename D, typename Container = std::set<D>,
362+
typename Range = std::initializer_list<D>,
361363
typename = std::enable_if_t<is_iterable_over_v<Range, D>>>
362364
auto killManyFlows(Range &&FactsToKill) {
363365
struct KillMany final : public FlowFunction<D, Container> {
@@ -465,7 +467,8 @@ auto generateFlowAndKillAllOthers(psr::type_identity_t<D> FactToGenerate,
465467
/// v v v ... \ ...
466468
/// x w v1 v2 ... vN u
467469
///
468-
template <typename D, typename Range, typename Container = std::set<D>,
470+
template <typename D, typename Container = std::set<D>,
471+
typename Range = std::initializer_list<D>,
469472
typename = std::enable_if_t<is_iterable_over_v<Range, D>>>
470473
auto generateManyFlowsAndKillAllOthers(Range &&FactsToGenerate, D From) {
471474
struct GenManyAndKillAllOthers final : public FlowFunction<D, Container> {

include/phasar/PhasarLLVM/Utils/LLVMIRToSrc.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,32 @@ class Function;
2929
class Value;
3030
class GlobalVariable;
3131
class Module;
32+
class DIFile;
3233
} // namespace llvm
3334

3435
namespace psr {
3536

36-
std::string getVarNameFromIR(const llvm::Value *V);
37+
[[nodiscard]] std::string getVarNameFromIR(const llvm::Value *V);
3738

38-
std::string getFunctionNameFromIR(const llvm::Value *V);
39+
[[nodiscard]] std::string getFunctionNameFromIR(const llvm::Value *V);
3940

40-
std::string getFilePathFromIR(const llvm::Value *V);
41+
[[nodiscard]] std::string getFilePathFromIR(const llvm::Value *V);
4142

42-
std::string getDirectoryFromIR(const llvm::Value *V);
43+
[[nodiscard]] std::string getDirectoryFromIR(const llvm::Value *V);
4344

44-
unsigned int getLineFromIR(const llvm::Value *V);
45+
[[nodiscard]] const llvm::DIFile *getDIFileFromIR(const llvm::Value *V);
4546

46-
unsigned int getColumnFromIR(const llvm::Value *V);
47+
[[nodiscard]] unsigned int getLineFromIR(const llvm::Value *V);
4748

48-
std::string getSrcCodeFromIR(const llvm::Value *V);
49+
[[nodiscard]] unsigned int getColumnFromIR(const llvm::Value *V);
4950

50-
std::string getModuleIDFromIR(const llvm::Value *V);
51+
[[nodiscard]] std::pair<unsigned, unsigned>
52+
getLineAndColFromIR(const llvm::Value *V);
53+
54+
[[nodiscard]] std::string getSrcCodeFromIR(const llvm::Value *V,
55+
bool Trim = true);
56+
57+
[[nodiscard]] std::string getModuleIDFromIR(const llvm::Value *V);
5158

5259
struct SourceCodeInfo {
5360
std::string SourceCodeLine;
@@ -76,7 +83,7 @@ void from_json(const nlohmann::json &J, SourceCodeInfo &Info);
7683
/// SourceCodeInfo
7784
void to_json(nlohmann::json &J, const SourceCodeInfo &Info);
7885

79-
SourceCodeInfo getSrcCodeInfoFromIR(const llvm::Value *V);
86+
[[nodiscard]] SourceCodeInfo getSrcCodeInfoFromIR(const llvm::Value *V);
8087

8188
} // namespace psr
8289

include/phasar/Utils/MaybeUniquePtr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class MaybeUniquePtr : detail::MaybeUniquePtrBase<T, RequireAlignment> {
7979
: detail::MaybeUniquePtrBase<T, RequireAlignment>(
8080
std::exchange(Other.Data, {})) {}
8181

82-
void swap(MaybeUniquePtr &Other) noexcept { std::swap(Data, Other, Data); }
82+
void swap(MaybeUniquePtr &Other) noexcept { std::swap(Data, Other.Data); }
8383

8484
friend void swap(MaybeUniquePtr &LHS, MaybeUniquePtr &RHS) noexcept {
8585
LHS.swap(RHS);

lib/PhasarLLVM/Utils/LLVMIRToSrc.cpp

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,6 @@ llvm::DILocation *getDILocation(const llvm::Value *V) {
103103
return nullptr;
104104
}
105105

106-
llvm::DIFile *getDIFile(const llvm::Value *V) {
107-
if (const auto *GO = llvm::dyn_cast<llvm::GlobalObject>(V)) {
108-
if (auto *MN = GO->getMetadata(llvm::LLVMContext::MD_dbg)) {
109-
if (auto *Subpr = llvm::dyn_cast<llvm::DISubprogram>(MN)) {
110-
return Subpr->getFile();
111-
}
112-
if (auto *GVExpr = llvm::dyn_cast<llvm::DIGlobalVariableExpression>(MN)) {
113-
return GVExpr->getVariable()->getFile();
114-
}
115-
}
116-
} else if (const auto *Arg = llvm::dyn_cast<llvm::Argument>(V)) {
117-
if (auto *LocVar = getDILocalVariable(Arg)) {
118-
return LocVar->getFile();
119-
}
120-
} else if (const auto *I = llvm::dyn_cast<llvm::Instruction>(V)) {
121-
if (I->isUsedByMetadata()) {
122-
if (auto *LocVar = getDILocalVariable(I)) {
123-
return LocVar->getFile();
124-
}
125-
} else if (I->getMetadata(llvm::LLVMContext::MD_dbg)) {
126-
return I->getDebugLoc()->getFile();
127-
}
128-
}
129-
return nullptr;
130-
}
131-
132106
std::string getVarNameFromIR(const llvm::Value *V) {
133107
if (auto *LocVar = getDILocalVariable(V)) {
134108
return LocVar->getName().str();
@@ -154,7 +128,7 @@ std::string getFunctionNameFromIR(const llvm::Value *V) {
154128
}
155129

156130
std::string getFilePathFromIR(const llvm::Value *V) {
157-
if (auto *DIF = getDIFile(V)) {
131+
if (auto *DIF = getDIFileFromIR(V)) {
158132
std::filesystem::path File(DIF->getFilename().str());
159133
std::filesystem::path Dir(DIF->getDirectory().str());
160134
if (!File.empty()) {
@@ -181,32 +155,58 @@ std::string getFilePathFromIR(const llvm::Value *V) {
181155
return "";
182156
}
183157

184-
unsigned int getLineFromIR(const llvm::Value *V) {
158+
const llvm::DIFile *getDIFileFromIR(const llvm::Value *V) {
159+
if (const auto *GO = llvm::dyn_cast<llvm::GlobalObject>(V)) {
160+
if (auto *MN = GO->getMetadata(llvm::LLVMContext::MD_dbg)) {
161+
if (auto *Subpr = llvm::dyn_cast<llvm::DISubprogram>(MN)) {
162+
return Subpr->getFile();
163+
}
164+
if (auto *GVExpr = llvm::dyn_cast<llvm::DIGlobalVariableExpression>(MN)) {
165+
return GVExpr->getVariable()->getFile();
166+
}
167+
}
168+
} else if (const auto *Arg = llvm::dyn_cast<llvm::Argument>(V)) {
169+
if (auto *LocVar = getDILocalVariable(Arg)) {
170+
return LocVar->getFile();
171+
}
172+
} else if (const auto *I = llvm::dyn_cast<llvm::Instruction>(V)) {
173+
if (I->isUsedByMetadata()) {
174+
if (auto *LocVar = getDILocalVariable(I)) {
175+
return LocVar->getFile();
176+
}
177+
} else if (I->getMetadata(llvm::LLVMContext::MD_dbg)) {
178+
return I->getDebugLoc()->getFile();
179+
}
180+
}
181+
return nullptr;
182+
}
183+
184+
std::string getDirectoryFromIR(const llvm::Value *V) {
185185
// Argument and Instruction
186186
if (auto *DILoc = getDILocation(V)) {
187-
return DILoc->getLine();
187+
return DILoc->getDirectory().str();
188188
}
189189
if (auto *DISubpr = getDISubprogram(V)) { // Function
190-
return DISubpr->getLine();
190+
return DISubpr->getDirectory().str();
191191
}
192192
if (auto *DIGV = getDIGlobalVariable(V)) { // Globals
193-
return DIGV->getLine();
193+
return DIGV->getDirectory().str();
194194
}
195-
return 0;
195+
return "";
196196
}
197197

198-
std::string getDirectoryFromIR(const llvm::Value *V) {
198+
unsigned int getLineFromIR(const llvm::Value *V) {
199199
// Argument and Instruction
200200
if (auto *DILoc = getDILocation(V)) {
201-
return DILoc->getDirectory().str();
201+
return DILoc->getLine();
202202
}
203203
if (auto *DISubpr = getDISubprogram(V)) { // Function
204-
return DISubpr->getDirectory().str();
204+
return DISubpr->getLine();
205205
}
206206
if (auto *DIGV = getDIGlobalVariable(V)) { // Globals
207-
return DIGV->getDirectory().str();
207+
return DIGV->getLine();
208208
}
209-
return "";
209+
return 0;
210210
}
211211

212212
unsigned int getColumnFromIR(const llvm::Value *V) {
@@ -217,7 +217,21 @@ unsigned int getColumnFromIR(const llvm::Value *V) {
217217
return 0;
218218
}
219219

220-
std::string getSrcCodeFromIR(const llvm::Value *V) {
220+
std::pair<unsigned, unsigned> getLineAndColFromIR(const llvm::Value *V) {
221+
// Argument and Instruction
222+
if (auto *DILoc = getDILocation(V)) {
223+
return {DILoc->getLine(), DILoc->getColumn()};
224+
}
225+
if (auto *DISubpr = getDISubprogram(V)) { // Function
226+
return {DISubpr->getLine(), 0};
227+
}
228+
if (auto *DIGV = getDIGlobalVariable(V)) { // Globals
229+
return {DIGV->getLine(), 0};
230+
}
231+
return {0, 0};
232+
}
233+
234+
std::string getSrcCodeFromIR(const llvm::Value *V, bool Trim) {
221235
unsigned int LineNr = getLineFromIR(V);
222236
if (LineNr > 0) {
223237
std::filesystem::path Path(getFilePathFromIR(V));
@@ -230,7 +244,7 @@ std::string getSrcCodeFromIR(const llvm::Value *V) {
230244
Ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
231245
}
232246
std::getline(Ifs, SrcLine);
233-
return llvm::StringRef(SrcLine).trim().str();
247+
return Trim ? llvm::StringRef(SrcLine).trim().str() : SrcLine;
234248
}
235249
}
236250
}

0 commit comments

Comments
 (0)