Skip to content

Instantly share code, notes, and snippets.

@ronen
Last active June 6, 2016 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronen/28d109efe74006e9d4dd576655f445db to your computer and use it in GitHub Desktop.
Save ronen/28d109efe74006e9d4dd576655f445db to your computer and use it in GitHub Desktop.
Demo of halide Metal AOT backend initialization/termination bug
#include "Halide.h"
class Dummy: public Halide::Generator<Dummy>
{
public:
Halide::ImageParam input{Halide::UInt(8), 3, "input"};
Halide::Var x, y, c;
Halide::Func build()
{
Halide::Func filter;
filter(x, y, c) = input(x, y, c);
return filter;
}
};
Halide::RegisterGenerator<Dummy> dummy{"dummy"};
int main()
{
return 0;
}
CXXFLAGS = -std=c++11 -g
HALIDE_TOOLS_DIR = /usr/local/share/halide/tools
default: demo
.PHONY: clean
demo: build/main.o build/halide_dummy_metal.o
$(CXX) $(CXXFLAGS) -o $@ $^ -framework Cocoa -framework Metal
clean:
rm -rf build
build/halide_%_metal.o: halide_%.cpp
build/%_metal.o build/%_metal.h: build/generate_%
$^ -e o -o build -f $(basename $(notdir $@)) target=metal
build/generate_halide_%: halide_%.cpp $(HALIDE_TOOLS_DIR)/GenGen.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -fno-rtti $^ -o $@ -lHalide
build/%.o: %.cpp
@mkdir -p $(dir $@)
$(CXX) -c $(CXXFLAGS) -o $@ $<
@ronen
Copy link
Author

ronen commented Jun 6, 2016

Stripped down this to a trivial test case: A main() that doesn't even call the halide function, but gets an assertion error

[...build path...]/src/runtime/metal.cpp:243 Assert failed: queue != 0

The files are available in https://gist.github.com/ronen/28d109efe74006e9d4dd576655f445db. In particular, notice that main.cpp is just:

int main()
{
    return 0;
}

And you'll see the filter generator is also trivial. Generating the filter with -e o target=metal and linking the .o's leads to a program that gets an assertion failure upon exit. To reproduce, grab the files from the gist, then:

$ make
c++ -c -std=c++11 -g -o build/main.o main.cpp
c++ -std=c++11 -g -fno-rtti halide_dummy.cpp /usr/local/share/halide/tools/GenGen.cpp -o build/generate_halide_dummy -lHalide
build/generate_halide_dummy -e  -o build -f halide_dummy_metal target=metal
c++ -std=c++11 -g -o demo build/main.o build/halide_dummy_metal.o -framework Cocoa -framework Metal
rm build/generate_halide_dummy
$ ./demo
[...build path...]/src/runtime/metal.cpp:243 Assert failed: queue != 0
$ lldb ./demo
(lldb) target create "./demo"
Current executable set to './demo' (x86_64).
(lldb) r
Process 41034 launched: './demo' (x86_64)
/private/tmp/halide-adobe-20160603-48598-1qbs7sx/src/runtime/metal.cpp:243 Assert failed: queue != 0
Process 41034 stopped
* thread #1: tid = 0x3457be, 0x00007fff96194f06 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00007fff96194f06 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
->  0x7fff96194f06 <+10>: jae    0x7fff96194f10            ; <+20>
    0x7fff96194f08 <+12>: movq   %rax, %rdi
    0x7fff96194f0b <+15>: jmp    0x7fff9618f7cd            ; cerror_nocancel
    0x7fff96194f10 <+20>: retq
(lldb) bt
* thread #1: tid = 0x3457be, 0x00007fff96194f06 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00007fff96194f06 libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00007fff9355f4ec libsystem_pthread.dylib`pthread_kill + 90
    frame #2: 0x00007fff88c2e6e7 libsystem_c.dylib`abort + 129
    frame #3: 0x00000001000060c7 demo`halide_metal_acquire_context + 231
    frame #4: 0x0000000100006451 demo`halide_metal_device_release + 33
    frame #5: 0x00007fff5fc133bc dyld`ImageLoaderMachO::doTermination(ImageLoader::LinkContext const&) + 212
    frame #6: 0x00007fff5fc0218b dyld`dyld::runAllStaticTerminators(void*) + 67
    frame #7: 0x00007fff88c2f46b libsystem_c.dylib`__cxa_finalize_ranges + 345
    frame #8: 0x00007fff88c2f76f libsystem_c.dylib`exit + 55
    frame #9: 0x00007fff92c7a5b4 libdyld.dylib`start + 8
    frame #10: 0x00007fff92c7a5ad libdyld.dylib`start + 1
(lldb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment