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

Fixes for top-of-tree LLVM (#6546) #6548

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,25 @@ $(BUILD_DIR)/initmod.%_32_debug.ll: $(SRC_DIR)/runtime/%.cpp $(BUILD_DIR)/clang_
@mkdir -p $(@D)
$(CLANG) $(CXX_WARNING_FLAGS) -g -DDEBUG_RUNTIME -O3 $(RUNTIME_CXX_FLAGS) -fpic -m32 -target $(RUNTIME_TRIPLE_32) -DCOMPILING_HALIDE_RUNTIME -DBITS_32 -emit-llvm -S $(SRC_DIR)/runtime/$*.cpp -o $@ -MMD -MP -MF $(BUILD_DIR)/initmod.$*_32_debug.d

ifneq (,$(findstring $(LLVM_VERSION_TIMES_10), 120 130))
# For LLVM14+, we must add elementtype() annotations to some of our LLVM IR;
# earlier versions either don't understand that keyword at all, or don't support
# the uses we have for it. Rather than forking these sources, for now we'll just
# edit the files at build time to remove the offending uses. Note that while we could use `sed`
# here, that isn't an option for CMake builds (since they must support Windows environments without
# such tooling); to ensure consistent transformations in all builds, we'll use the tool here, too.
#
# (This may well need attention in the future, depending on how the LLVM opaque-pointers work proceeeds;
# see https://llvm.org/docs/OpaquePointers.html)
$(BUILD_DIR)/initmod.%_ll.ll: $(SRC_DIR)/runtime/%.ll $(BIN_DIR)/regexp_replace
@mkdir -p $(@D)
$(BIN_DIR)/regexp_replace 'elementtype\(i[0-9]+\)' '' < $(SRC_DIR)/runtime/$*.ll > $(BUILD_DIR)/initmod.$*_ll.ll
else
$(BUILD_DIR)/initmod.%_ll.ll: $(SRC_DIR)/runtime/%.ll
@mkdir -p $(@D)
cp $(SRC_DIR)/runtime/$*.ll $(BUILD_DIR)/initmod.$*_ll.ll
endif


$(BUILD_DIR)/initmod.%.bc: $(BUILD_DIR)/initmod.%.ll $(BUILD_DIR)/llvm_ok
$(LLVM_AS) $(BUILD_DIR)/initmod.$*.ll -o $(BUILD_DIR)/initmod.$*.bc
Expand All @@ -1088,6 +1104,10 @@ $(BIN_DIR)/binary2cpp: $(ROOT_DIR)/tools/binary2cpp.cpp
@mkdir -p $(@D)
$(CXX) $< -o $@

$(BIN_DIR)/regexp_replace: $(ROOT_DIR)/tools/regexp_replace.cpp
@mkdir -p $(@D)
$(CXX) -std=c++11 $< -o $@
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++17, surely


$(BUILD_DIR)/initmod_ptx.%_ll.o: $(BUILD_DIR)/initmod_ptx.%_ll.cpp
$(CXX) -c $< -o $@ -MMD -MP -MF $(BUILD_DIR)/$*.d -MT $(BUILD_DIR)/$*.o

Expand Down
27 changes: 25 additions & 2 deletions src/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,32 @@ foreach (i IN LISTS RUNTIME_LL)
set(BC "initmod.${i}.bc")
set(INITMOD "_initmod_${i}.cpp")

# For LLVM14+, we must add elementtype() annotations to some of our LLVM IR;
# earlier versions either don't understand that keyword at all, or don't support
# the uses we have for it. Rather than forking these sources, for now we'll just
# edit the files at build time to remove the offending uses. Note that `sed` and friends aren't
# an option for CMake builds (since they must support Windows environments without
# such tooling).
#
# (This may well need attention in the future, depending on how the LLVM opaque-pointers work proceeeds;
# see https://llvm.org/docs/OpaquePointers.html)

set(LL_TRANSFORMED "${LL}.transformed.ll")
if (Halide_LLVM_VERSION VERSION_LESS 14.0)
add_custom_command(OUTPUT "${LL_TRANSFORMED}"
COMMAND regexp_replace "elementtype\\(i[0-9]+\\)" "" < "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${LL}>" > "${LL_TRANSFORMED}"
DEPENDS "${LL}" regexp_replace
VERBATIM)
else ()
add_custom_command(OUTPUT "${LL_TRANSFORMED}"
COMMAND ${CMAKE_COMMAND} -E copy "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${LL}>" "${LL_TRANSFORMED}"
DEPENDS "${LL}"
VERBATIM)
endif ()

add_custom_command(OUTPUT "${BC}"
COMMAND llvm-as "$<SHELL_PATH:${CMAKE_CURRENT_SOURCE_DIR}/${LL}>" -o "${BC}"
DEPENDS "${LL}"
COMMAND llvm-as "${LL_TRANSFORMED}" -o "${BC}"
DEPENDS "${LL_TRANSFORMED}"
VERBATIM)
add_custom_command(OUTPUT "${INITMOD}"
COMMAND binary2cpp "halide_internal_initmod_${i}_ll" < "${BC}" > "${INITMOD}"
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/hvx_128.ll
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ define weak_odr void @halide.hexagon.vscatter_acc.w.w(i8* %buf_ptr, i32 %size, <
}

define weak_odr void @halide.hexagon.scatter.release(i8* %ptr) nounwind uwtable {
call void asm sideeffect "vmem($0 + #0):scatter_release\0A; v1 = vmem($0 + #0)\0A", "=*m,*m,~{v1}"(i8* %ptr, i8* %ptr)
call void asm sideeffect "vmem($0 + #0):scatter_release\0A; v1 = vmem($0 + #0)\0A", "=*m,*m,~{v1}"(i8* elementtype(i8) %ptr, i8* elementtype(i8) %ptr)
ret void
}

Expand Down
8 changes: 2 additions & 6 deletions src/runtime/x86.ll
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
; Note that this is only used for LLVM 8.0+
define weak_odr <16 x i8> @paddusbx16(<16 x i8> %a0, <16 x i8> %a1) nounwind alwaysinline {
%1 = add <16 x i8> %a0, %a1
%2 = icmp ugt <16 x i8> %a0, %1
%3 = select <16 x i1> %2, <16 x i8> <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>, <16 x i8> %1
ret <16 x i8> %3
}

; Note that this is only used for LLVM 8.0+
define weak_odr <8 x i16> @padduswx8(<8 x i16> %a0, <8 x i16> %a1) nounwind alwaysinline {
%1 = add <8 x i16> %a0, %a1
%2 = icmp ugt <8 x i16> %a0, %1
%3 = select <8 x i1> %2, <8 x i16> <i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1>, <8 x i16> %1
ret <8 x i16> %3
}

; Note that this is only used for LLVM 8.0+
define weak_odr <16 x i8> @psubusbx16(<16 x i8> %a0, <16 x i8> %a1) nounwind alwaysinline {
%1 = icmp ugt <16 x i8> %a0, %a1
%2 = select <16 x i1> %1, <16 x i8> %a0, <16 x i8> %a1
%3 = sub <16 x i8> %2, %a1
ret <16 x i8> %3
}

; Note that this is only used for LLVM 8.0+
define weak_odr <8 x i16> @psubuswx8(<8 x i16> %a0, <8 x i16> %a1) nounwind alwaysinline {
%1 = icmp ugt <8 x i16> %a0, %a1
%2 = select <8 x i1> %1, <8 x i16> %a0, <8 x i16> %a1
Expand Down Expand Up @@ -125,12 +121,12 @@ define weak_odr <4 x float> @fast_inverse_sqrt_f32x4(<4 x float> %x) nounwind uw
; -- A version without stack spills tends to confuse the x86-32 code generator
; and cause it to fail via running out of registers.
define weak_odr void @x86_cpuid_halide(i32* %info) nounwind uwtable {
call void asm sideeffect inteldialect "xchg ebx, esi\0A\09mov eax, dword ptr $$0 $0\0A\09mov ecx, dword ptr $$4 $0\0A\09cpuid\0A\09mov dword ptr $$0 $0, eax\0A\09mov dword ptr $$4 $0, ebx\0A\09mov dword ptr $$8 $0, ecx\0A\09mov dword ptr $$12 $0, edx\0A\09xchg ebx, esi", "=*m,~{eax},~{ebx},~{ecx},~{edx},~{esi},~{dirflag},~{fpsr},~{flags}"(i32* %info)
call void asm sideeffect inteldialect "xchg ebx, esi\0A\09mov eax, dword ptr $$0 $0\0A\09mov ecx, dword ptr $$4 $0\0A\09cpuid\0A\09mov dword ptr $$0 $0, eax\0A\09mov dword ptr $$4 $0, ebx\0A\09mov dword ptr $$8 $0, ecx\0A\09mov dword ptr $$12 $0, edx\0A\09xchg ebx, esi", "=*m,~{eax},~{ebx},~{ecx},~{edx},~{esi},~{dirflag},~{fpsr},~{flags}"(i32* elementtype(i32) %info)
ret void
}

; Same, but ensure that we save/restore the full 64 bits of rbx/rsi.
define weak_odr void @x64_cpuid_halide(i32* %info) nounwind uwtable {
call void asm sideeffect inteldialect "xchg rbx, rsi\0A\09mov eax, dword ptr $$0 $0\0A\09mov ecx, dword ptr $$4 $0\0A\09cpuid\0A\09mov dword ptr $$0 $0, eax\0A\09mov dword ptr $$4 $0, ebx\0A\09mov dword ptr $$8 $0, ecx\0A\09mov dword ptr $$12 $0, edx\0A\09xchg rbx, rsi", "=*m,~{eax},~{ebx},~{ecx},~{edx},~{esi},~{dirflag},~{fpsr},~{flags}"(i32* %info)
call void asm sideeffect inteldialect "xchg rbx, rsi\0A\09mov eax, dword ptr $$0 $0\0A\09mov ecx, dword ptr $$4 $0\0A\09cpuid\0A\09mov dword ptr $$0 $0, eax\0A\09mov dword ptr $$4 $0, ebx\0A\09mov dword ptr $$8 $0, ecx\0A\09mov dword ptr $$12 $0, edx\0A\09xchg rbx, rsi", "=*m,~{eax},~{ebx},~{ecx},~{edx},~{esi},~{dirflag},~{fpsr},~{flags}"(i32* elementtype(i32) %info)
ret void
}
3 changes: 3 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ target_compile_options(build_halide_h PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/wd4996>
add_executable(binary2cpp binary2cpp.cpp)
target_compile_options(binary2cpp PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/wd4996>)

add_executable(regexp_replace regexp_replace.cpp)
target_compile_options(regexp_replace PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/wd4996>)

##
# Interface target for enabling PNG/JPEG support in Halide
##
Expand Down
25 changes: 25 additions & 0 deletions tools/regexp_replace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <cassert>
#include <cstring>
#include <iostream>
#include <regex>

// A utility that does a single regexp-based replace on stdin and dumps it to stdout.
// Exists solely because we can't rely on (e.g.) `sed` being available in Windows
// build environments. Usage is basically equivalent to `sed -e 's/regex/replacement/g'`
// Note that if regex is an empty string, this becomes a simple line-by-line file copy.

int main(int argc, const char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s regex replacement\n", argv[0]);
return -1;
}
std::regex re(argv[1]);

std::string line;
while (std::getline(std::cin, line)) {
std::regex_replace(std::ostreambuf_iterator<char>(std::cout),
line.begin(), line.end(), re, argv[2]);
std::cout << std::endl;
}
return 0;
}