Skip to content

Commit

Permalink
update C API bindings to LLVM 14
Browse files Browse the repository at this point in the history
 * zig_clang is fully updated
 * zig_llvm is fully updated

Some initial work on codegen.cpp is in place for upgrading to LLVM's
new opaque pointers. However there is much more to be done.

A few of zig llvm bindings for deprecated functions have been updated;
more need to be updated.
  • Loading branch information
andrewrk committed Feb 3, 2022
1 parent 397e055 commit 5466e87
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 76 deletions.
4 changes: 2 additions & 2 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ pub const Target = struct {
hsail64,
spir,
spir64,
spirv32,
spirv64,
kalimba,
shave,
lanai,
Expand All @@ -815,8 +817,6 @@ pub const Target = struct {
// Stage1 currently assumes that architectures above this comment
// map one-to-one with the ZigLLVM_ArchType enum.
spu_2,
spirv32,
spirv64,

pub fn isX86(arch: Arch) bool {
return switch (arch) {
Expand Down
8 changes: 6 additions & 2 deletions src/clang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1104,20 +1104,20 @@ pub const TypeClass = enum(c_int) {
VariableArray,
Atomic,
Attributed,
BitInt,
BlockPointer,
Builtin,
Complex,
Decltype,
Auto,
DeducedTemplateSpecialization,
DependentAddressSpace,
DependentExtInt,
DependentBitInt,
DependentName,
DependentSizedExtVector,
DependentTemplateSpecialization,
DependentVector,
Elaborated,
ExtInt,
FunctionNoProto,
FunctionProto,
InjectedClassName,
Expand Down Expand Up @@ -1146,6 +1146,7 @@ pub const TypeClass = enum(c_int) {
Typedef,
UnaryTransform,
UnresolvedUsing,
Using,
Vector,
ExtVector,
};
Expand Down Expand Up @@ -1187,6 +1188,7 @@ const StmtClass = enum(c_int) {
OMPDistributeSimdDirectiveClass,
OMPForDirectiveClass,
OMPForSimdDirectiveClass,
OMPGenericLoopDirectiveClass,
OMPMasterTaskLoopDirectiveClass,
OMPMasterTaskLoopSimdDirectiveClass,
OMPParallelForDirectiveClass,
Expand All @@ -1210,6 +1212,7 @@ const StmtClass = enum(c_int) {
OMPUnrollDirectiveClass,
OMPMaskedDirectiveClass,
OMPMasterDirectiveClass,
OMPMetaDirectiveClass,
OMPOrderedDirectiveClass,
OMPParallelDirectiveClass,
OMPParallelMasterDirectiveClass,
Expand Down Expand Up @@ -1746,6 +1749,7 @@ pub const BuiltinTypeKind = enum(c_int) {
Float16,
BFloat16,
Float128,
Ibm128,
NullPtr,
ObjCId,
ObjCClass,
Expand Down
15 changes: 8 additions & 7 deletions src/codegen/llvm/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,11 @@ pub const Module = opaque {
pub const getLastGlobalAlias = LLVMGetLastGlobalAlias;
extern fn LLVMGetLastGlobalAlias(M: *const Module) *const Value;

pub const addAlias = LLVMAddAlias;
extern fn LLVMAddAlias(
pub const addAlias = LLVMAddAlias2;
extern fn LLVMAddAlias2(
M: *const Module,
Ty: *const Type,
AddrSpace: c_uint,
Aliasee: *const Value,
Name: [*:0]const u8,
) *const Value;
Expand Down Expand Up @@ -458,8 +459,8 @@ pub const Builder = opaque {
pub const buildStore = LLVMBuildStore;
extern fn LLVMBuildStore(*const Builder, Val: *const Value, Ptr: *const Value) *const Value;

pub const buildLoad = LLVMBuildLoad;
extern fn LLVMBuildLoad(*const Builder, PointerVal: *const Value, Name: [*:0]const u8) *const Value;
pub const buildLoad = LLVMBuildLoad2;
extern fn LLVMBuildLoad2(*const Builder, Ty: *const Type, PointerVal: *const Value, Name: [*:0]const u8) *const Value;

pub const buildNeg = LLVMBuildNeg;
extern fn LLVMBuildNeg(*const Builder, V: *const Value, Name: [*:0]const u8) *const Value;
Expand Down Expand Up @@ -1067,9 +1068,9 @@ pub extern fn LLVMInitializeM68kAsmParser() void;
pub extern fn LLVMInitializeCSKYAsmParser() void;
pub extern fn LLVMInitializeVEAsmParser() void;

extern fn ZigLLDLinkCOFF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
extern fn ZigLLDLinkELF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
extern fn ZigLLDLinkWasm(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool) c_int;
extern fn ZigLLDLinkCOFF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool, disable_output: bool) c_int;
extern fn ZigLLDLinkELF(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool, disable_output: bool) c_int;
extern fn ZigLLDLinkWasm(argc: c_int, argv: [*:null]const ?[*:0]const u8, can_exit_early: bool, disable_output: bool) c_int;

pub const LinkCOFF = ZigLLDLinkCOFF;
pub const LinkELF = ZigLLDLinkELF;
Expand Down
21 changes: 12 additions & 9 deletions src/stage1/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {

for (size_t i = 1; i < fn->export_list.length; i += 1) {
GlobalExport *fn_export = &fn->export_list.items[i];
LLVMAddAlias(g->module, LLVMTypeOf(llvm_fn), llvm_fn, buf_ptr(&fn_export->name));
LLVMAddAlias2(g->module, LLVMTypeOf(llvm_fn), 0, llvm_fn, buf_ptr(&fn_export->name));
}
}

Expand Down Expand Up @@ -901,10 +901,10 @@ static LLVMValueRef gen_store(CodeGen *g, LLVMValueRef value, LLVMValueRef ptr,
return gen_store_untyped(g, value, ptr, alignment, ptr_type->data.pointer.is_volatile);
}

static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alignment, bool is_volatile,
const char *name)
static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMTypeRef elem_type, LLVMValueRef ptr,
uint32_t alignment, bool is_volatile, const char *name)
{
LLVMValueRef result = LLVMBuildLoad(g->builder, ptr, name);
LLVMValueRef result = LLVMBuildLoad2(g->builder, elem_type, ptr, name);
if (is_volatile) LLVMSetVolatile(result, true);
if (alignment == 0) {
LLVMSetAlignment(result, LLVMABIAlignmentOfType(g->target_data_ref, LLVMGetElementType(LLVMTypeOf(ptr))));
Expand All @@ -916,8 +916,10 @@ static LLVMValueRef gen_load_untyped(CodeGen *g, LLVMValueRef ptr, uint32_t alig

static LLVMValueRef gen_load(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_type, const char *name) {
assert(ptr_type->id == ZigTypeIdPointer);
ZigType *elem_type = ptr_type->data.pointer.child_type;
uint32_t alignment = get_ptr_align(g, ptr_type);
return gen_load_untyped(g, ptr, alignment, ptr_type->data.pointer.is_volatile, name);
return gen_load_untyped(g, get_llvm_type(g, elem_type), ptr, alignment,
ptr_type->data.pointer.is_volatile, name);
}

static LLVMValueRef get_handle_value(CodeGen *g, LLVMValueRef ptr, ZigType *type, ZigType *ptr_type) {
Expand Down Expand Up @@ -1265,15 +1267,16 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
size_t len_field_index = slice_type->data.structure.fields[slice_len_index]->gen_index;
LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");

LLVMValueRef len_value = gen_load_untyped(g, len_field_ptr, 0, false, "");
LLVMValueRef index_val = gen_load_untyped(g, index_field_ptr, 0, false, "");
LLVMValueRef len_value = gen_load_untyped(g, usize_type_ref, len_field_ptr, 0, false, "");
LLVMValueRef index_val = gen_load_untyped(g, usize_type_ref, index_field_ptr, 0, false, "");
LLVMValueRef len_val_minus_one = LLVMBuildSub(g->builder, len_value, LLVMConstInt(usize_type_ref, 1, false), "");
LLVMValueRef masked_val = LLVMBuildAnd(g->builder, index_val, len_val_minus_one, "");
LLVMValueRef address_indices[] = {
masked_val,
};

LLVMValueRef ptr_value = gen_load_untyped(g, ptr_field_ptr, 0, false, "");
LLVMTypeRef ptr_to_usize = LLVMPointerType(usize_type_ref, 0);
LLVMValueRef ptr_value = gen_load_untyped(g, ptr_to_usize, ptr_field_ptr, 0, false, "");
LLVMValueRef address_slot = LLVMBuildInBoundsGEP(g->builder, ptr_value, address_indices, 1, "");

gen_store_untyped(g, address_value, address_slot, 0, false);
Expand Down Expand Up @@ -8802,7 +8805,7 @@ static void do_code_gen(CodeGen *g) {

for (size_t export_i = 1; export_i < var->export_list.length; export_i += 1) {
GlobalExport *global_export = &var->export_list.items[export_i];
LLVMAddAlias(g->module, LLVMTypeOf(var->value_ref), var->value_ref, buf_ptr(&global_export->name));
LLVMAddAlias2(g->module, LLVMTypeOf(var->value_ref), 0, var->value_ref, buf_ptr(&global_export->name));
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/stage1/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ static const ZigLLVM_ArchType arch_list[] = {
ZigLLVM_hsail64, // AMD HSAIL with 64-bit pointers
ZigLLVM_spir, // SPIR: standard portable IR for OpenCL 32-bit version
ZigLLVM_spir64, // SPIR: standard portable IR for OpenCL 64-bit version
ZigLLVM_spirv32, // SPIR-V with 32-bit pointers
ZigLLVM_spirv64, // SPIR-V with 64-bit pointers
ZigLLVM_kalimba, // Kalimba: generic kalimba
ZigLLVM_shave, // SHAVE: Movidius vector VLIW processors
ZigLLVM_lanai, // Lanai: Lanai 32-bit
Expand Down
16 changes: 12 additions & 4 deletions src/zig_clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,14 @@ void ZigClang_detect_enum_TypeClass(clang::Type::TypeClass ty) {
case clang::Type::DependentSizedArray:
case clang::Type::DependentSizedExtVector:
case clang::Type::DependentAddressSpace:
case clang::Type::DependentExtInt:
case clang::Type::DependentBitInt:
case clang::Type::Vector:
case clang::Type::DependentVector:
case clang::Type::ExtVector:
case clang::Type::FunctionProto:
case clang::Type::FunctionNoProto:
case clang::Type::UnresolvedUsing:
case clang::Type::Using:
case clang::Type::Paren:
case clang::Type::Typedef:
case clang::Type::MacroQualified:
Expand All @@ -315,8 +316,8 @@ void ZigClang_detect_enum_TypeClass(clang::Type::TypeClass ty) {
case clang::Type::Record:
case clang::Type::Enum:
case clang::Type::Elaborated:
case clang::Type::ExtInt:
case clang::Type::Attributed:
case clang::Type::BitInt:
case clang::Type::TemplateTypeParm:
case clang::Type::SubstTemplateTypeParm:
case clang::Type::SubstTemplateTypeParmPack:
Expand Down Expand Up @@ -345,20 +346,20 @@ static_assert((clang::Type::TypeClass)ZigClangType_IncompleteArray == clang::Typ
static_assert((clang::Type::TypeClass)ZigClangType_VariableArray == clang::Type::VariableArray, "");
static_assert((clang::Type::TypeClass)ZigClangType_Atomic == clang::Type::Atomic, "");
static_assert((clang::Type::TypeClass)ZigClangType_Attributed == clang::Type::Attributed, "");
static_assert((clang::Type::TypeClass)ZigClangType_BitInt == clang::Type::BitInt, "");
static_assert((clang::Type::TypeClass)ZigClangType_BlockPointer == clang::Type::BlockPointer, "");
static_assert((clang::Type::TypeClass)ZigClangType_Builtin == clang::Type::Builtin, "");
static_assert((clang::Type::TypeClass)ZigClangType_Complex == clang::Type::Complex, "");
static_assert((clang::Type::TypeClass)ZigClangType_Decltype == clang::Type::Decltype, "");
static_assert((clang::Type::TypeClass)ZigClangType_Auto == clang::Type::Auto, "");
static_assert((clang::Type::TypeClass)ZigClangType_DeducedTemplateSpecialization == clang::Type::DeducedTemplateSpecialization, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentAddressSpace == clang::Type::DependentAddressSpace, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentExtInt == clang::Type::DependentExtInt, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentBitInt == clang::Type::DependentBitInt, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentName == clang::Type::DependentName, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentSizedExtVector == clang::Type::DependentSizedExtVector, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentTemplateSpecialization == clang::Type::DependentTemplateSpecialization, "");
static_assert((clang::Type::TypeClass)ZigClangType_DependentVector == clang::Type::DependentVector, "");
static_assert((clang::Type::TypeClass)ZigClangType_Elaborated == clang::Type::Elaborated, "");
static_assert((clang::Type::TypeClass)ZigClangType_ExtInt == clang::Type::ExtInt, "");
static_assert((clang::Type::TypeClass)ZigClangType_FunctionNoProto == clang::Type::FunctionNoProto, "");
static_assert((clang::Type::TypeClass)ZigClangType_FunctionProto == clang::Type::FunctionProto, "");
static_assert((clang::Type::TypeClass)ZigClangType_InjectedClassName == clang::Type::InjectedClassName, "");
Expand Down Expand Up @@ -387,6 +388,7 @@ static_assert((clang::Type::TypeClass)ZigClangType_TypeOf == clang::Type::TypeOf
static_assert((clang::Type::TypeClass)ZigClangType_Typedef == clang::Type::Typedef, "");
static_assert((clang::Type::TypeClass)ZigClangType_UnaryTransform == clang::Type::UnaryTransform, "");
static_assert((clang::Type::TypeClass)ZigClangType_UnresolvedUsing == clang::Type::UnresolvedUsing, "");
static_assert((clang::Type::TypeClass)ZigClangType_Using == clang::Type::Using, "");
static_assert((clang::Type::TypeClass)ZigClangType_Vector == clang::Type::Vector, "");
static_assert((clang::Type::TypeClass)ZigClangType_ExtVector == clang::Type::ExtVector, "");

Expand Down Expand Up @@ -429,6 +431,7 @@ void ZigClang_detect_enum_StmtClass(clang::Stmt::StmtClass x) {
case clang::Stmt::OMPDistributeSimdDirectiveClass:
case clang::Stmt::OMPForDirectiveClass:
case clang::Stmt::OMPForSimdDirectiveClass:
case clang::Stmt::OMPGenericLoopDirectiveClass:
case clang::Stmt::OMPMasterTaskLoopDirectiveClass:
case clang::Stmt::OMPMasterTaskLoopSimdDirectiveClass:
case clang::Stmt::OMPParallelForDirectiveClass:
Expand All @@ -452,6 +455,7 @@ void ZigClang_detect_enum_StmtClass(clang::Stmt::StmtClass x) {
case clang::Stmt::OMPUnrollDirectiveClass:
case clang::Stmt::OMPMaskedDirectiveClass:
case clang::Stmt::OMPMasterDirectiveClass:
case clang::Stmt::OMPMetaDirectiveClass:
case clang::Stmt::OMPOrderedDirectiveClass:
case clang::Stmt::OMPParallelDirectiveClass:
case clang::Stmt::OMPParallelMasterDirectiveClass:
Expand Down Expand Up @@ -654,6 +658,7 @@ static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPDistributeParallelForSimdD
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPDistributeSimdDirectiveClass == clang::Stmt::OMPDistributeSimdDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPForDirectiveClass == clang::Stmt::OMPForDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPForSimdDirectiveClass == clang::Stmt::OMPForSimdDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPGenericLoopDirectiveClass == clang::Stmt::OMPGenericLoopDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPMasterTaskLoopDirectiveClass == clang::Stmt::OMPMasterTaskLoopDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPMasterTaskLoopSimdDirectiveClass == clang::Stmt::OMPMasterTaskLoopSimdDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPParallelForDirectiveClass == clang::Stmt::OMPParallelForDirectiveClass, "");
Expand All @@ -677,6 +682,7 @@ static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPTileDirectiveClass == clan
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPUnrollDirectiveClass == clang::Stmt::OMPUnrollDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPMaskedDirectiveClass == clang::Stmt::OMPMaskedDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPMasterDirectiveClass == clang::Stmt::OMPMasterDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPMetaDirectiveClass == clang::Stmt::OMPMetaDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPOrderedDirectiveClass == clang::Stmt::OMPOrderedDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPParallelDirectiveClass == clang::Stmt::OMPParallelDirectiveClass, "");
static_assert((clang::Stmt::StmtClass)ZigClangStmt_OMPParallelMasterDirectiveClass == clang::Stmt::OMPParallelMasterDirectiveClass, "");
Expand Down Expand Up @@ -1269,6 +1275,7 @@ void ZigClang_detect_enum_BuiltinTypeKind(clang::BuiltinType::Kind x) {
case clang::BuiltinType::Float16:
case clang::BuiltinType::BFloat16:
case clang::BuiltinType::Float128:
case clang::BuiltinType::Ibm128:
case clang::BuiltinType::NullPtr:
case clang::BuiltinType::ObjCId:
case clang::BuiltinType::ObjCClass:
Expand Down Expand Up @@ -1510,6 +1517,7 @@ static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeLongDouble == clang::
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeFloat16 == clang::BuiltinType::Float16, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeBFloat16 == clang::BuiltinType::BFloat16, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeFloat128 == clang::BuiltinType::Float128, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeIbm128 == clang::BuiltinType::Ibm128, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeNullPtr == clang::BuiltinType::NullPtr, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeObjCId == clang::BuiltinType::ObjCId, "");
static_assert((clang::BuiltinType::Kind)ZigClangBuiltinTypeObjCClass == clang::BuiltinType::ObjCClass, "");
Expand Down
8 changes: 6 additions & 2 deletions src/zig_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,20 @@ enum ZigClangTypeClass {
ZigClangType_VariableArray,
ZigClangType_Atomic,
ZigClangType_Attributed,
ZigClangType_BitInt,
ZigClangType_BlockPointer,
ZigClangType_Builtin,
ZigClangType_Complex,
ZigClangType_Decltype,
ZigClangType_Auto,
ZigClangType_DeducedTemplateSpecialization,
ZigClangType_DependentAddressSpace,
ZigClangType_DependentExtInt,
ZigClangType_DependentBitInt,
ZigClangType_DependentName,
ZigClangType_DependentSizedExtVector,
ZigClangType_DependentTemplateSpecialization,
ZigClangType_DependentVector,
ZigClangType_Elaborated,
ZigClangType_ExtInt,
ZigClangType_FunctionNoProto,
ZigClangType_FunctionProto,
ZigClangType_InjectedClassName,
Expand Down Expand Up @@ -293,6 +293,7 @@ enum ZigClangTypeClass {
ZigClangType_Typedef,
ZigClangType_UnaryTransform,
ZigClangType_UnresolvedUsing,
ZigClangType_Using,
ZigClangType_Vector,
ZigClangType_ExtVector,
};
Expand Down Expand Up @@ -334,6 +335,7 @@ enum ZigClangStmtClass {
ZigClangStmt_OMPDistributeSimdDirectiveClass,
ZigClangStmt_OMPForDirectiveClass,
ZigClangStmt_OMPForSimdDirectiveClass,
ZigClangStmt_OMPGenericLoopDirectiveClass,
ZigClangStmt_OMPMasterTaskLoopDirectiveClass,
ZigClangStmt_OMPMasterTaskLoopSimdDirectiveClass,
ZigClangStmt_OMPParallelForDirectiveClass,
Expand All @@ -357,6 +359,7 @@ enum ZigClangStmtClass {
ZigClangStmt_OMPUnrollDirectiveClass,
ZigClangStmt_OMPMaskedDirectiveClass,
ZigClangStmt_OMPMasterDirectiveClass,
ZigClangStmt_OMPMetaDirectiveClass,
ZigClangStmt_OMPOrderedDirectiveClass,
ZigClangStmt_OMPParallelDirectiveClass,
ZigClangStmt_OMPParallelMasterDirectiveClass,
Expand Down Expand Up @@ -893,6 +896,7 @@ enum ZigClangBuiltinTypeKind {
ZigClangBuiltinTypeFloat16,
ZigClangBuiltinTypeBFloat16,
ZigClangBuiltinTypeFloat128,
ZigClangBuiltinTypeIbm128,
ZigClangBuiltinTypeNullPtr,
ZigClangBuiltinTypeObjCId,
ZigClangBuiltinTypeObjCClass,
Expand Down
Loading

0 comments on commit 5466e87

Please sign in to comment.