How to remove unused basicblocks llvm - llvm

I'm trying to remove unused blocks from llvm intermediate code.
First, I build a collection of reachable basicblocks, by starting with the entry basicblock and adding all basicblock successors from their terminator instruction:
bbset.insert(bbterm->getSuccessor(i));
After that, I iterate through the unreachable basicblocks and delete them using
DeleteDeadBlock(bb);
from Utils/BasicBlockUtils.cpp
It works, but in some scenarios I get a segfault. Below is the core dump:
opt: /home/user/llvm-3.3.src/lib/Transforms/Utils/BasicBlockUtils.cpp:38: void llvm::DeleteDeadBlock(llvm::BasicBlock*): Assertion `(pred_begin(BB) == pred_end(BB) || BB->getSinglePredecessor() == BB) && "Block is not dead!"' failed.
0 opt 0x0000000001756825 llvm::sys::PrintStackTrace(_IO_FILE*) + 38
1 opt 0x0000000001756aa2
2 opt 0x000000000175651e
3 libpthread.so.0 0x00007fe1fe76dbb0
4 libc.so.6 0x00007fe1fdbaef77 gsignal + 55
5 libc.so.6 0x00007fe1fdbb25e8 abort + 328
6 libc.so.6 0x00007fe1fdba7d43
7 libc.so.6 0x00007fe1fdba7df2
8 opt 0x000000000138b004 llvm::DeleteDeadBlock(llvm::BasicBlock*) + 139
9 opt 0x0000000000d49c2e
10 opt 0x0000000001652942 llvm::FPPassManager::runOnFunction(llvm::Function&) + 290
11 opt 0x0000000001652ab2 llvm::FPPassManager::runOnModule(llvm::Module&) + 84
12 opt 0x0000000001652dd0 llvm::MPPassManager::runOnModule(llvm::Module&) + 502
13 opt 0x00000000016533a0 llvm::PassManagerImpl::run(llvm::Module&) + 244
14 opt 0x00000000016535ab llvm::PassManager::run(llvm::Module&) + 39
15 opt 0x00000000008c6cb6 main + 5698
16 libc.so.6 0x00007fe1fdb99de5 __libc_start_main + 245
17 opt 0x00000000008b8a39
Any ideas what could be the problem?

That's not a segfault, that's an assertion error, and it's telling you the problem: the predecessor list is not empty. You get this assertion error because DeleteDeadBlock verifies that the block you are deleting has no immediate predecessors (other than itself).
However, this test does not mean that the block is alive; for example, in this scenario (assume lines are downward-facing arrows):
entry
/
A B
| |
C D
\ /
E
B and D are both dead, but if you'll try to remove D before B you'll get the assertion since D has a predecessor (which isn't itself).
How to solve this? You can do one of:
Process the blocks in topological order
Remove all predecessors from the block you are about to delete (e.g. by replacing the terminator of all its predecessors with the unreachable instruction)
You can also do that to all the blocks you identify as dead first, and then delete them in arbitrary order

You can use llvm::EliminateUnreachableBlocks. It performs a depth-first-search of basic blocks, starting at the entry block. This helps identify 'live' blocks that would become 'dead' once their 'dead' predecessors are removed.

Related

bash: clean and merge data

I have three csv files containing different data for a common object. These represent data about distinct collections of items at work. These objects have unique codes. The number of files is not important so I will set this problem up with two. I have a handy recipe for joining these files using join -- but the cleaning part is killing me.
File A snippet - contains unique data. Also the cataloging error E B.
B 547
J 65
EB 289
E B 1
CO 8900
ZX 7
File B snippet - unique data about a different dimension of the objects.
B 5
ZX 67
SD 4
CO 76
J 54
EB 10
Note that file B contains a code not in common with file A.
Now I submit to you the "official" canon of codes designated for this set of objects:
B
CO
ZX
J
EB
Note that File B contains a non-canonical code with data. It needs to be captured and documented. Same with bad code in file A.
End goal: run trend and stats on the collections using the various fields from the multiple reports. They mostly match the canon but there are oddballs due to cataloging errors and codes that are no longer in use.
End goal result after merge/join:
B 547 5
J 65 54
EB 289 10
CO 8900 76
ZX 7 67
So my first idea was to use grep -F -f for this, using the canonical codes as a search list then merge with join. Problem is, with one letter codes it's too inclusive. It would seem like a job for awk where it can work with tab delimiters and REGEX the oddball codes. I'm not sure though, how to get awk to use a list to sift other files. Will join alone handle all this? Maybe I merge with join or paste, then sift out the weirdos? Which method is the least brittle and more likely to handle edge cases like the drunk cataloger?
If you're thinking, "Dude, this is better done with Perl or Python ...etc.". I'm all ears. No rules, I just need to deliver!
Your question says the data is csv, but based on your samples I'm assuming it's tsv. I'm also assuming E B should end up in the outlier output and that NA values should be filled with 0.
Given those assumptions, the following may be sufficient:
sort -t $'\t' -k 1b,1 fileA > fileA.sorted && sort -t $'\t' -k 1b,1 fileB > fileB.sorted
join -t $'\t' -a1 -a2 -e0 -o auto fileA.sorted fileB.sorted > out
grep -f codes out > out-canon
grep -vf codes out > out-oddball
The content of file codes:
^B\s
^CO\s
^ZX\s
^J\s
^EB\s
Result:
$ cat out-canon
B 547 5
CO 8900 76
EB 289 10
J 65 54
ZX 7 67
$ cat out-oddball
E B 1 0
SD 0 4
Try this(GNU awk):
awk 'BEGIN{FS=OFS="\t";}ARGIND==1{c[$1]++;}ARGIND==2{b[$1]=$2}ARGIND==3{if (c[$1]) {print $1,$2,b[$1]+0; delete b[$1];} else {if(tolower($1)~"[a-z]+ +[a-z]+")print>"error.fileA"; else print>"oddball.fileA";}}END{for (i in b) {print i,0,b[i] " (? maybe?)";print i,b[i] > "oddball.fileB";}}' codes fileB fileA
It will create error.fileA, oddball.fileA if such lines exists, oddball.fileB.
Normal output didn't write to file, you can write with > yourself when results are ok:
B 547 5
J 65 54
EB 289 10
CO 8900 76
ZX 7 67
SD 0 4 (? maybe?)
Had a hard time reading your description, not sure if this is what you want.
Anyway it's easy to improve this awk code.
You can change to FILENAME=="file1", or FILENAME==ARGV[1] if ARGIND is not working.

Archive project in Swift 3 and get "command failed due to signal segmentation fault 11"

I updated my code for Swift 3, run on devices and everything works, but when I Archive project, after cleaning and deleting Delivered Data, I have error "command failed due to signal segmentation fault 11"
My logs
0 swift 0x000000010a5ffb6d PrintStackTraceSignalHandler(void*) + 45
1 swift 0x000000010a5ff5b6 SignalHandler(int) + 470
2 libsystem_platform.dylib 0x00007fff9560152a _sigtramp + 26
3 libsystem_platform.dylib 0x00007fff5825ab50 _sigtramp + 3267728960
4 swift 0x0000000107d29432 swift::CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(swift::UnconditionalCheckedCastAddrInst*) + 1554
5 swift 0x0000000107db144d processFunction(swift::SILFunction&, bool, unsigned int) + 1901
6 swift 0x0000000107db7f9f (anonymous namespace)::ConstantPropagation::run() + 47
7 swift 0x0000000107d4862d swift::SILPassManager::runOneIteration() + 6077
8 swift 0x0000000107d4d7d6 swift::runSILOptimizationPasses(swift::SILModule&) + 3462
9 swift 0x0000000107a153cb performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef, int&, swift::FrontendObserver*) + 20107
10 swift 0x0000000107a0e265 swift::performFrontend(llvm::ArrayRef, char const*, void*, swift::FrontendObserver*) + 17029
11 swift 0x00000001079cb82d main + 8685
12 libdyld.dylib 0x00007fff8936c5ad start + 1
13 libdyld.dylib 0x000000000000006d start + 1992899265
Stack dump:
and this
While running pass #1059521 SILFunctionTransform "Constant Propagation" on SILFunction "#_TTSg5VSC29UIApplicationLaunchOptionsKeyS_s8Hashable5UIKit_P__CSo8NSObjectS2_S0_10ObjectiveC_Ps9AnyObject____TFs17_dictionaryUpCastu2_Rxs8Hashable0_S_rFGVs10Dictionaryxq__GS0_q0_q1__".
Can someone help or tell, where should I search bug?
In my case I was passing launchOptions as function parameter to another class inside
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
After migrating to Swift 3, migrator added cast to match awaited parameter's type, which was of type [NSObject : AnyObject]? (pre Swift 3)
All I had to do is just update my custom function to take [UIApplicationLaunchOptionsKey: Any] as parameter and remove that false cast. Simple as that
I didn't have to compromise any optimization level (which shouldn't even be the last resort for developers, this is definitely not a way how to solve compiler errors/segmentation faults). After removing cast all is working fine with swift's whole module optimization.

Erlang - Open file read line delete line and save

I need to open a file, read the line, do some thing, delete that line save, open again the same file and continue with the rest
so far i have this code ..
1 -module(setup_data).
2 -export([for_each_line_in_file/1]).
3
4 for_each_line_in_file(Name) ->
5 {ok, Device} = file:open(Name, [read]),
6 for_each_line(Device).
7
8 for_each_line(Device) ->
9 case io:get_line(Device, "") of
10 eof -> file:close(Device);
11 Line ->
12 do_something(Line)
13 for_each_line(Device)
14 end.
So I want something like
1 -module(setup_data).
2 -export([for_each_line_in_file/1]).
3
4 for_each_line_in_file(Name) ->
4
6 for_each_line(Name).
7
8 for_each_line(Device) ->
9 {ok, Device} = file:open(Name, [read]),
9 case io:get_line(Device, "") of
10 eof -> file:close(Device);
11 Line ->
12 io:format("LINE : ...... ~p~n",[Line]),
23
43 /DELETE THAT CURRENT LINE AND SAVE?
33 file:close(Device)
13 for_each_line(Name)
14 end.
To elaborate on Armon comment:
In server fault q&a site, there was a post about how fast certain operations are. Disk seek for the first line in your file takes 10ms, while reading 1Mb of sequential data lasts 30ms. If your file has 300 lines, your solutions would work 10 times slower and if it has 3000 lines, it would be 100 times slower than reading 1Mb od data.
In this situation, read entire file to memory as a binary, do what you need to do with the lines and finally save the file again. You can distribute work between processes, but I wouldn't bother, because accessing disk is probably the longest operation in your code.

Fortran95 -- Reading from a formatted text file

I need to read some values from a table. These are the first five rows, to give you some idea of what it should look like:
1 + 3 98 96 1
2 + 337 2799 2463 1
3 + 2801 3733 933 1
4 + 3734 5020 1287 1
5 + 5234 5530 297 1
My interest is in the first four columns of each row. I need to read these into arrays. I used the following code:
program ----
implicit none
integer, parameter :: totbases = 4639675, totgenes = 4395
integer :: codtot, ks
integer, dimension(totgenes) :: ngene, lend, rend
character :: genome*4639675, sign*4
open(1,file='e_coli_g_info')
open(2,file='e_coli_g_str')
do ks = 1, totgenes
read(1,100) ngene(ks),sign(ks:ks),lend(ks), rend(ks)
end do
100 format(1x,i4,8x,a1, 2(5x,i7), 22x)
do ks = 1, 100
write(*,*) ngene(ks), sign(ks:ks),lend(ks), rend(ks)
end do
end program
The loop at the end of the program is to print the first hundred entries to test that they are being read correctly. The problem is that I am getting this garbage (the fourth row is the problem):
1 + 3 757934891
2 + 337 724249387
3 + 2801 757803819
4 + 3734 757803819
5 + 5234 757935405
Clearly, the fourth column is way off. In fact, I cannot find these values anywhere in the file that I am reading from. I am using the gfortran compiler for Ubuntu 12.04. I would greatly appreciate if somebody would point me in the right direction. I'm sure it's likely that I'm missing something very obvious because I'm new at Fortran.
Fortran formats are (traditionally, there's some newer stuff that I won't go into here) fixed format, that is, they are best suited for file formats with fixed columns. I.e. column N always starts at character position M, no ifs or buts. If your file format is more "free format"-like, that is, columns are separated by whitespace, it's often easier and more robust to read data using list formatting. That is, try to do your read loop as
do ks = 1, totgenes
read(1, *) ngene(ks), sign(ks:ks), lend(ks), rend(ks)
end do
Also, as a general advice, when opening your own files, start from unit 10 and go upwards from there. Fortran implementations typically use some of the low-numbered units for standard input, output, and error (a common choice is units 1, 5, and 6). You probably don't want to redirect those.
PS 2: I haven't tried your code, but it seems that you have a bounds overflow in the sign variable. It's declared of length 4, but then you assign to index ks which goes all the way up to totgenes. As you're using gfortran on Ubuntu 12.04 (that is, gfortran 4.6), when developing compile with options "-O1 -Wall -g -fcheck=all"

cocos2d box2d sprite position crash

I have the following code:
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL)
{
CCSprite *itemSprite = (CCSprite*)b->GetUserData();
CGSize SpriteSize;
SpriteSize.height = itemSprite.contentSize.height;
SpriteSize.width = itemSprite.contentSize.width;
CGPoint SpritePosition=[itemSprite position];
}
}
Just get the position of sprite, but this program crash sometimes in:
SpriteSize.height = itemSprite.contentSize.height;
and this line:
CGPoint SpritePosition=[itemSprite position];
I am using cocos2d .99.5.
Edit: here is my crash log:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000f
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x00002666 objc_msgSend_stret + 14
1 ShootTheMonkey 0x0004c066 -[LevelFour ccTouchBegan:withEvent:] (LevelFour.mm:1432)
2 ShootTheMonkey 0x00321bf0 -[CCTouchDispatcher touches:withEvent:withTouchType:] (CCTouchDispatcher.m:238)
3 ShootTheMonkey 0x00322454 -[CCTouchDispatcher touchesBegan:withEvent:] (CCTouchDispatcher.m:305)
4 ShootTheMonkey 0x003240ec -[EAGLView touchesBegan:withEvent:] (EAGLView.m:318)
5 UIKit 0x00053d72 -[UIWindow _sendTouchesForEvent:] + 254
6 UIKit 0x000536fe -[UIWindow sendEvent:] + 74
7 UIKit 0x0004f320 -[UIApplication sendEvent:] + 260
8 UIKit 0x0004ec8c _UIApplicationHandleEvent + 4772
9 GraphicsServices 0x00003b2c PurpleEventCallback + 660
10 CoreFoundation 0x00022d96 CFRunLoopRunSpecific + 2214
11 CoreFoundation 0x000224da CFRunLoopRunInMode + 42
12 GraphicsServices 0x000030d4 GSEventRunModal + 108
13 GraphicsServices 0x00003180 GSEventRun + 56
14 UIKit 0x0000342a -[UIApplication _run] + 374
15 UIKit 0x00001954 UIApplicationMain + 636
16 ShootTheMonkey 0x0000377e main (main.m:13)
17 ShootTheMonkey 0x0000373c start + 32
Since you do not post the crash dump or stack trace, I can only guess. AFAIK there are at least two reasons:
You assign objects of multiple classes to body->SetUserData(). The crash happens when you cast the user data to CCSprite when it's in fact not a CCSprite. Although I doubt this is the case since usually people assign objects of the same class to it.
The second reason is more likely to happen due to the way people usually depend on CCLayer to retain its CCSprite children (rather than explicitly retaining them). When you remove the CCSprites from the parent CCLayer, the latter releases the former and if retain count == 0 (very likely) the CCSprites are released from the heap. But remember that some of b2Bodies are still holding the pointers to these CCSprites through the user data attributes? So what happen when you iterate over all the bodies and try to use the released CCSprites that are still attached to the user data? Crash!
The way to fix this is that you need to remove the bodies when their corresponding sprites are removed from their CCLayer, by either subclassing CCSprite to add an ivar that points to b2Body, or by wrapping both objects (i.e sprite and body) in another class. Then add code to remove the body when the sprite is about to be de-alloc-ed.
Hope this helps. If not, please post more details (crash dump or stack trace).
Well, what we know for sure is the sprite was assigned to userData and it was later deallocated. I see that your assigned sprite was an autoreleased object and more likely it means that out of the scope sprite has received a release massage. To make sure that it is true, try to create sprite as follows: [[CCSprite alloc] initWithSprite:#"apple.png"]; But remember later you must release it.
If it worked for you, use the suggestion that above.