LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 1435 - licm breaks volatile store in multiply-nested loops
Summary: licm breaks volatile store in multiply-nested loops
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Global Analyses (show other bugs)
Version: 1.0
Hardware: All All
: P normal
Assignee: Chris Lattner
URL:
Keywords: miscompilation
Depends on:
Blocks:
 
Reported: 2007-05-17 17:47 PDT by Zhiru Zhang
Modified: 2010-03-06 13:59 PST (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments
test_volatile.c (449 bytes, text/plain)
2007-05-17 17:52 PDT, Zhiru Zhang
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zhiru Zhang 2007-05-17 17:47:43 PDT
I know that there is some recent discussions about the optimizations on 
volatile loads and stores, and the conclusion is that LLVM is doing the right 
thing. But this time the story seems different. 

See below for details. The test case will be posted shortly. 

=======================================================================
For my first example, LLVM is able to preserve both volatile load and store 
correctly. 

void PassThrough(volatile int* DataIn, volatile int* DataOut)
{
  int i;
  int buffer[64];

  for (i = 0; i < 64; i++)
    buffer[i] = *DataIn;

  for (i = 0; i < 64; i++)
    *DataOut = buffer[i];
}

=======================================================================
For the second one, however, LICM will move the volatile store outside of the 
loop, which is apparently wrong. 

void Transpose(volatile int* DataIn, volatile int* DataOut)
{
  int i, j;
  int buffer[64];

  for (i = 0; i < 64; i++)
    buffer[i] = *DataIn;

  for (i = 0; i < 8; i++)
    for (j = 0; j < 8; j++)
      *DataOut = buffer[j * 8 + i];
}

=========================================================
What I did:

llvm-gcc -O0 -c -emit-llvm test_volatile.c -o test.bc
opt -mem2reg -licm test.bc -o new.bc -f
Comment 1 Zhiru Zhang 2007-05-17 17:52:25 PDT
Created attachment 862 [details]
test_volatile.c
Comment 2 Chris Lattner 2007-05-17 18:49:53 PDT
This sounds like a real bug.
Comment 3 Chris Lattner 2007-05-23 01:38:11 PDT
Nice catch.  This impacts nested loops only.  Thanks for the excellent testcase reduction.

Fixed.  Testcase here: Transforms/LICM/2007-05-22-VolatileSink.ll

Patch here:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20070521/049945.html

-Chris