Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Fixed bug where shifting by out-of-bounds value results in no compute code being emitted. #5115

Merged
merged 3 commits into from
Mar 23, 2020

Conversation

dpankratz
Copy link
Contributor

Bug

For operations << and >> when the RHS is an IntImm with values that are negative or greater than or equal to the number of bits in the integer being shifted results in LLVM emitting no code for the tvm.compute.

Example

a = te.var(name='a', dtype='int32')
shape = (1,)
c = te.compute(shape,lambda i: (4 * a) + (a >> 33))
s = te.create_schedule([c.op])
f = tvm.build(s,[a,c])
print("default_function_compute" in f.get_source()) #prints False
c_tvm= tvm.nd.array(np.zeros(shape,dtype='int32'))
f(5,c_tvm) 
print(c_tvm.asnumpy()[0]) #prints 0 instead of 20

Fix

Limit the RHS immediate values of << and >> to be within a valid range. For example the new behaviour would be:

a = te.var(name='a', dtype='int64')
a << 64 #raises TVMError
a << -1 #raises TVMError
a >> 31 #no error

Explanation

LLVM constant folding silently replaces an out-of-bounds shift to an undef as seen here. Then any other parts of the expression are composed with the undef ( (4 * a) + undef -> undef in above example) and become undef as well. Therefore the tvm.compute is producing an undef which is optimized away by LLVM resulting in no compute code being generated at all.

A review would be much appreciated!

@tqchen

@dpankratz dpankratz changed the title [Bugfix] Fixed bug where shifting by out-of-bounds RHS values results in LLVM … [Bugfix] Fixed bug where shifting by out-of-bounds value results in no compute code being emitted. Mar 20, 2020
@tqchen
Copy link
Member

tqchen commented Mar 20, 2020

cc @vinx13 @ZihengJiang @merrymercy @yzhliu please also help to take a look

@tqchen tqchen merged commit a422589 into apache:master Mar 23, 2020
@tqchen
Copy link
Member

tqchen commented Mar 23, 2020

Thanks @dpankratz !

trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Apr 16, 2020
…o compute code being emitted. (apache#5115)

* Fixed bug where shifting by out-of-bounds RHS values results in LLVM to codegen nothing. Added regression testcase

* Updated testcase to be more precise.

* Fixed testcase
zhiics pushed a commit to neo-ai/tvm that referenced this pull request Apr 17, 2020
…o compute code being emitted. (apache#5115)

* Fixed bug where shifting by out-of-bounds RHS values results in LLVM to codegen nothing. Added regression testcase

* Updated testcase to be more precise.

* Fixed testcase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants