Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LLDB] Add basic floating point ops to IR interpreter
Patch adds support for fadd, fsub, fdiv, fmul and fcmp to IR interpreter. ~~~ OS Laboratory. Huawei RRI. Saint-Petersburg Reviewed By: clayborg Differential Revision: https://reviews.llvm.org/D126359
- Loading branch information
Pavel Kosov
committed
Aug 10, 2022
1 parent
c122658
commit f63e2cf
Showing
4 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
C_SOURCES := main.c | ||
|
||
include Makefile.rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
"""Tests IR interpreter handling of basic floating point operations (fadd, fsub, etc).""" | ||
|
||
import lldb | ||
from lldbsuite.test.decorators import * | ||
from lldbsuite.test.lldbtest import * | ||
from lldbsuite.test import lldbutil | ||
|
||
class FPEvalTestCase(TestBase): | ||
|
||
mydir = TestBase.compute_mydir(__file__) | ||
|
||
def setUp(self): | ||
# Call super's setUp(). | ||
TestBase.setUp(self) | ||
self.jit_opts = lldb.SBExpressionOptions() | ||
self.jit_opts.SetAllowJIT(True) | ||
self.no_jit_opts = lldb.SBExpressionOptions() | ||
self.no_jit_opts.SetAllowJIT(False) | ||
# Find the line number to break inside main(). | ||
self.line = line_number('main.c', '// Set break point at this line.') | ||
|
||
def test(self): | ||
"""Test floating point expressions while jitter is disabled.""" | ||
self.build() | ||
exe = self.getBuildArtifact("a.out") | ||
self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) | ||
|
||
# Break inside the main. | ||
lldbutil.run_break_set_by_file_and_line( | ||
self, "main.c", self.line, num_expected_locations=1, loc_exact=True) | ||
|
||
|
||
value = self.frame().EvaluateExpression("a + b", self.no_jit_opts) | ||
|
||
self.runCmd("run", RUN_SUCCEEDED) | ||
# test double | ||
self.expect("expr --allow-jit false -- a + b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['double', '44']) | ||
self.expect("expr --allow-jit false -- a - b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['double', '40']) | ||
self.expect("expr --allow-jit false -- a / b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['double', '21']) | ||
self.expect("expr --allow-jit false -- a * b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['double', '84']) | ||
self.expect("expr --allow-jit false -- a + 2", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['double', '44']) | ||
self.expect("expr --allow-jit false -- a > b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
self.expect("expr --allow-jit false -- a >= b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
self.expect("expr --allow-jit false -- a < b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- a <= b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- a == b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- a != b", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
|
||
# test single | ||
self.expect("expr --allow-jit false -- f + q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['float', '44']) | ||
self.expect("expr --allow-jit false -- f - q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['float', '40']) | ||
self.expect("expr --allow-jit false -- f / q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['float', '21']) | ||
self.expect("expr --allow-jit false -- f * q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['float', '84']) | ||
self.expect("expr --allow-jit false -- f + 2", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['float', '44']) | ||
self.expect("expr --allow-jit false -- f > q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
self.expect("expr --allow-jit false -- f >= q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
self.expect("expr --allow-jit false -- f < q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- f <= q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- f == q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['false']) | ||
self.expect("expr --allow-jit false -- f != q", VARIABLES_DISPLAYED_CORRECTLY, | ||
substrs=['true']) | ||
|
||
# compare jit and interpreter output | ||
self.assertTrue(self.process().IsValid()) | ||
thread = lldbutil.get_stopped_thread(self.process(), lldb.eStopReasonBreakpoint) | ||
self.assertTrue(thread.IsValid()) | ||
frame = thread.GetSelectedFrame() | ||
self.assertTrue(frame.IsValid()) | ||
|
||
dividents = [42, 79, 666] | ||
divisors = [1.618, 2.718281828, 3.1415926535, 6.62607015] | ||
|
||
for x in dividents: | ||
for y in divisors: | ||
vardef = "double x = {0}, y = {1};".format(x, y) | ||
v1 = frame.EvaluateExpression("{0}; eval(x, y, 2)".format(vardef), self.jit_opts) | ||
v2 = frame.EvaluateExpression("{0}; x / y".format(vardef), self.no_jit_opts) | ||
self.assertTrue(v1.IsValid() and v2.IsValid()) | ||
self.assertTrue(str(v1.GetData()) == str(v2.GetData())) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
double eval(double a, double b, int op) { | ||
switch (op) { | ||
case 0: return a+b; | ||
case 1: return a-b; | ||
case 2: return a/b; | ||
case 3: return a*b; | ||
default: return 0; | ||
} | ||
} | ||
|
||
int main (int argc, char const *argv[]) | ||
{ | ||
double a = 42.0, b = 2.0; | ||
float f = 42.0, q = 2.0; | ||
return 0; //// Set break point at this line. | ||
} |