Skip to content

Commit 52efb47

Browse files
committed
[fix] Put in order fp-runtime functions
1 parent b74bfa6 commit 52efb47

21 files changed

Lines changed: 124 additions & 131 deletions

File tree

include/klee/Support/ModuleUtil.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,16 @@ bool linkModules(llvm::Module *composite,
3939
std::vector<std::unique_ptr<llvm::Module>> &modules,
4040
const unsigned Flags, std::string &errorMsg);
4141

42-
#if defined(__x86_64__) || defined(__i386__)
43-
#define addFunctionReplacement(from, to) \
44-
{#from "f", #to "f"}, {#from "", #to ""}, { "" #from "l", #to "l" }
42+
void replaceOrRenameFunction(llvm::Module *module, const char *old_name,
43+
const char *new_name);
4544

45+
#if defined(__x86_64__) || defined(__i386__)
4646
#define addIntrinsicReplacement(from, to) \
4747
{"llvm." #from ".f32", #to "f"}, {"llvm." #from ".f64", #to}, { \
4848
"llvm." #from ".f80", #to "l" \
4949
}
5050

5151
#else
52-
#define addFunctionReplacement(from, to) \
53-
{#from "f", #to "f"}, { "" #from, "" #to }
54-
5552
#define addIntrinsicReplacement(from, to) \
5653
{"llvm." #from ".f32", #to "f"}, { "llvm." #from ".f64", #to }
5754

@@ -62,33 +59,15 @@ bool linkModules(llvm::Module *composite,
6259
/// implementations in runtime/klee-fp, but not explicitly replaced here. Should
6360
/// we rename them and complete the list?
6461
const std::vector<std::pair<std::string, std::string>> floatReplacements = {
65-
addFunctionReplacement(__isnan, klee_internal_isnan),
66-
addFunctionReplacement(isnan, klee_internal_isnan),
67-
addFunctionReplacement(__isinf, klee_internal_isinf),
68-
addFunctionReplacement(isinf, klee_internal_isinf),
69-
addFunctionReplacement(__fpclassify, klee_internal_fpclassify),
70-
addFunctionReplacement(fpclassify, klee_internal_fpclassify),
71-
addFunctionReplacement(__finite, klee_internal_finite),
72-
addFunctionReplacement(finite, klee_internal_finite),
73-
addFunctionReplacement(sqrt, klee_internal_sqrt),
74-
addFunctionReplacement(fabs, klee_internal_fabs),
75-
addFunctionReplacement(rint, klee_internal_rint),
76-
addFunctionReplacement(round, klee_internal_rint),
77-
addFunctionReplacement(__signbit, klee_internal_signbit),
78-
addIntrinsicReplacement(sqrt, klee_internal_sqrt),
79-
addIntrinsicReplacement(rint, klee_internal_rint),
80-
addIntrinsicReplacement(round, klee_internal_rint),
62+
addIntrinsicReplacement(sqrt, sqrt),
63+
addIntrinsicReplacement(rint, rint),
64+
addIntrinsicReplacement(round, rint),
8165
addIntrinsicReplacement(nearbyint, nearbyint),
8266
addIntrinsicReplacement(copysign, copysign),
83-
addIntrinsicReplacement(floor, klee_floor),
67+
addIntrinsicReplacement(floor, floor),
8468
addIntrinsicReplacement(ceil, ceil)};
85-
#undef addFunctionReplacement
8669
#undef addIntrinsicReplacement
8770

88-
const std::vector<std::pair<std::string, std::string>> feRoundReplacements{
89-
{"fegetround", "klee_internal_fegetround"},
90-
{"fesetround", "klee_internal_fesetround"}};
91-
9271
/// Return the Function* target of a Call or Invoke instruction, or
9372
/// null if it cannot be determined (should be only for indirect
9473
/// calls, although complicated constant expressions might be

lib/Module/KModule.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,6 @@ bool KModule::link(std::vector<std::unique_ptr<llvm::Module>> &modules,
235235
return true;
236236
}
237237

238-
void KModule::replaceFunction(const std::unique_ptr<llvm::Module> &m,
239-
const char *original, const char *replacement) {
240-
llvm::Function *originalFunc = m->getFunction(original);
241-
llvm::Function *replacementFunc = m->getFunction(replacement);
242-
if (!originalFunc)
243-
return;
244-
klee_message("Replacing function \"%s\" with \"%s\"", original, replacement);
245-
assert(replacementFunc && "Replacement function not found");
246-
assert(!(replacementFunc->isDeclaration()) && "replacement must have body");
247-
originalFunc->replaceAllUsesWith(replacementFunc);
248-
originalFunc->eraseFromParent();
249-
}
250-
251238
void KModule::instrument(const Interpreter::ModuleOptions &opts) {
252239
// Inject checks prior to optimization... we also perform the
253240
// invariant transformations that we will end up doing later so that
@@ -301,12 +288,10 @@ void KModule::optimiseAndPrepare(
301288
if (opts.WithFPRuntime) {
302289
if (UseKleeFloatInternals) {
303290
for (const auto &p : klee::floatReplacements) {
304-
replaceFunction(module, p.first.c_str(), p.second.c_str());
291+
replaceOrRenameFunction(module.get(), p.first.c_str(),
292+
p.second.c_str());
305293
}
306294
}
307-
for (const auto &p : klee::feRoundReplacements) {
308-
replaceFunction(module, p.first.c_str(), p.second.c_str());
309-
}
310295
}
311296

312297
// Needs to happen after linking (since ctors/dtors can be modified)

lib/Module/ModuleUtil.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,19 @@ bool klee::loadFileAsOneModule(
367367
}
368368
return res;
369369
}
370+
371+
void klee::replaceOrRenameFunction(llvm::Module *module, const char *old_name,
372+
const char *new_name) {
373+
Function *new_function, *old_function;
374+
new_function = module->getFunction(new_name);
375+
old_function = module->getFunction(old_name);
376+
if (old_function) {
377+
if (new_function) {
378+
old_function->replaceAllUsesWith(new_function);
379+
old_function->eraseFromParent();
380+
} else {
381+
old_function->setName(new_name);
382+
assert(old_function->getName() == new_name);
383+
}
384+
}
385+
}

runtime/klee-fp/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
set(LIB_PREFIX "RuntimeFp")
1111
set(SRC_FILES
1212
ceil.c
13-
klee_copysign.c
13+
copysign.c
1414
exp.c
1515
fabs.c
16-
klee_fenv.c
17-
klee_floor.c
16+
fenv.c
17+
floor.c
1818
fpclassify.c
19-
klee_internal_isinf.ll
20-
klee_rint.c
19+
isinf.ll
2120
klee_set_rounding_mode.c
22-
klee_signbit.ll
2321
log.c
22+
rint.c
2423
round.c
24+
signbit.ll
2525
sqrt.c
2626
trigonometry.c
2727
)

runtime/klee-fp/ceil.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88
//===----------------------------------------------------------------------===*/
99

1010
#include "float.h"
11-
#include "klee_floor.h"
12-
#include "klee_rint.h"
11+
#include "floor.h"
12+
#include "rint.h"
1313

1414
float ceilf(float f) {
15-
if (f == klee_internal_rintf(f)) {
15+
if (f == rintf(f)) {
1616
return f;
1717
}
18-
return ((f < 0.0f) ? -1 : 1) + klee_floorf(f);
18+
return ((f < 0.0f) ? -1 : 1) + floorf(f);
1919
}
2020

2121
double ceil(double f) {
22-
if (f == klee_internal_rint(f)) {
22+
if (f == rint(f)) {
2323
return f;
2424
}
25-
return ((f < 0.0f) ? -1 : 1) + klee_floor(f);
25+
return ((f < 0.0f) ? -1 : 1) + floor(f);
2626
}
2727

2828
long double ceill(long double f) {
29-
if (f == klee_internal_rintl(f)) {
29+
if (f == rintl(f)) {
3030
return f;
3131
}
32-
return ((f < 0.0f) ? -1 : 1) + klee_floorl(f);
32+
return ((f < 0.0f) ? -1 : 1) + floorl(f);
3333
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*===-- klee_copysign.c ---------------------------------------------------===//
1+
/*===-- copysign.c --------------------------------------------------------===//
22
//
33
// The KLEE Symbolic Virtual Machine
44
//
@@ -7,7 +7,7 @@
77
//
88
//===----------------------------------------------------------------------===*/
99

10-
#include "klee_copysign.h"
10+
#include "copysign.h"
1111
#include "ansidecl.h"
1212
#include "klee/klee.h"
1313

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*===-- klee_copysign.h ---------------------------------------------------===//
1+
/*===-- copysign.h --------------------------------------------------------===//
22
//
33
// The KLEE Symbolic Virtual Machine
44
//

runtime/klee-fp/fabs.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
//===----------------------------------------------------------------------===*/
99
#include "klee/klee.h"
1010

11-
double klee_internal_fabs(double d) { return klee_abs_double(d); }
11+
double fabs(double d) { return klee_abs_double(d); }
1212

13-
float klee_internal_fabsf(float f) { return klee_abs_float(f); }
13+
float fabsf(float f) { return klee_abs_float(f); }
1414

1515
#if defined(__x86_64__) || defined(__i386__)
16-
long double klee_internal_fabsl(long double f) {
17-
return klee_abs_long_double(f);
18-
}
16+
long double fabsl(long double f) { return klee_abs_long_double(f); }
1917
#endif
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/*===-- klee_fenv.c -------------------------------------------------------===//
1+
/*===-- fenv.c ------------------------------------------------------------===//
22
//
33
// The KLEE Symbolic Virtual Machine
44
//
55
// This file is distributed under the University of Illinois Open Source
66
// License. See LICENSE.TXT for details.
77
//
88
//===----------------------------------------------------------------------===*/
9-
#include "klee_fenv.h"
9+
#include "fenv.h"
1010
#include "klee/klee.h"
1111

1212
// Define the constants. Don't include `fenv.h` here to avoid
@@ -28,7 +28,7 @@ enum {
2828
#error Architecture not supported
2929
#endif
3030

31-
int klee_internal_fegetround(void) {
31+
int fegetround(void) {
3232
enum KleeRoundingMode rm = klee_get_rounding_mode();
3333
switch (rm) {
3434
case KLEE_FP_RNE:
@@ -52,7 +52,7 @@ int klee_internal_fegetround(void) {
5252
}
5353
}
5454

55-
int klee_internal_fesetround(int rm) {
55+
int fesetround(int rm) {
5656
switch (rm) {
5757
case FE_TONEAREST:
5858
klee_set_rounding_mode(KLEE_FP_RNE);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define KLEE_FENV_H
1212
#include "klee/klee.h"
1313

14-
int klee_internal_fegetround(void);
15-
int klee_internal_fesetround(int rm);
14+
int fegetround(void);
15+
int fesetround(int rm);
1616

1717
#endif // KLEE_FENV_H

0 commit comments

Comments
 (0)