Browse Source

Add new intrinsics: debug_trap, trap, read_cycle_counter, expect

gingerBill 4 years ago
parent
commit
47c7dc6a9b

+ 12 - 3
core/intrinsics/intrinsics.odin

@@ -6,6 +6,7 @@ package intrinsics
 
 x86_mmx :: x86_mmx; // Specialized SIMD Vector type
 
+// Types
 simd_vector :: proc($N: int, $T: typeid) -> type/#simd[N]T
 soa_struct :: proc($N: int, $T: typeid) -> type/#soa[N]T
 
@@ -13,8 +14,11 @@ soa_struct :: proc($N: int, $T: typeid) -> type/#soa[N]T
 volatile_load  :: proc(dst: ^$T) -> T ---
 volatile_store :: proc(dst: ^$T, val: T) -> T ---
 
-// Atomics
+// Trapping
+debug_trap :: proc() ---
+trap :: proc() -> ! ---
 
+// Atomics
 atomic_fence        :: proc() ---
 atomic_fence_acq    :: proc() ---
 atomic_fence_rel    :: proc() ---
@@ -89,9 +93,14 @@ atomic_cxchgweak_acqrel_failrelaxed :: proc(dst: ^$T, old, new: T) -> (T, /*opti
 
 // Instructions
 
-alloca :: proc(size, align: int) -> ^u8 ---
+alloca             :: proc(size, align: int) -> ^u8 ---
+cpu_relax          :: proc() ---
+read_cycle_counter :: proc() -> i64 ---
+
+
+// Compiler Hints
+expect :: proc(val, expected_val: T) -> T ---
 
-cpu_relax :: proc() ---
 
 // Constant type tests
 

+ 0 - 3
core/runtime/core.odin

@@ -433,9 +433,6 @@ typeid_base_without_enum :: typeid_core;
 
 @(default_calling_convention = "none")
 foreign {
-	@(link_name="llvm.assume")
-	assume :: proc(cond: bool) ---;
-
 	@(link_name="llvm.debugtrap")
 	debug_trap :: proc() ---;
 

+ 66 - 1
src/check_expr.cpp

@@ -5757,8 +5757,21 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
 		operand->mode = Addressing_NoValue;
 		break;
 
+	case BuiltinProc_trap:
+	case BuiltinProc_debug_trap:
+		if (!build_context.use_llvm_api) {
+			error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name));
+		}
+		operand->mode = Addressing_NoValue;
+		break;
 
-
+	case BuiltinProc_read_cycle_counter:
+		if (!build_context.use_llvm_api) {
+			error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name));
+		}
+		operand->mode = Addressing_Value;
+		operand->type = t_i64;
+		break;
 
 	case BuiltinProc_atomic_fence:
 	case BuiltinProc_atomic_fence_acq:
@@ -5963,6 +5976,58 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
 		break;
 
 
+	case BuiltinProc_expect:
+		if (!build_context.use_llvm_api) {
+			error(ce->args[0], "%.*s is not supported on this backend", LIT(builtin_procs[id].name));
+			return false;
+		} else {
+			Operand x = {};
+			Operand y = {};
+			check_expr(c, &x, ce->args[0]);
+			check_expr(c, &y, ce->args[1]);
+			if (x.mode == Addressing_Invalid) {
+				return false;
+			}
+			if (y.mode == Addressing_Invalid) {
+				return false;
+			}
+			convert_to_typed(c, &y, x.type);
+			convert_to_typed(c, &x, y.type);
+			if (!are_types_identical(x.type, y.type)) {
+				gbString xts = type_to_string(x.type);
+				gbString yts = type_to_string(y.type);
+				error(x.expr, "Mismatched types for %.*s, %s vs %s", LIT(builtin_procs[id].name), xts, yts);
+				gb_string_free(yts);
+				gb_string_free(xts);
+				*operand = x; // minimize error propagation
+				return true;
+			}
+
+			if (!is_type_integer_like(x.type)) {
+				gbString xts = type_to_string(x.type);
+				error(x.expr, "Values passed to %.*s must be an integer-like type (integer, boolean, enum, bit_set), got %s", LIT(builtin_procs[id].name), xts);
+				gb_string_free(xts);
+				*operand = x;
+				return true;
+			}
+
+			if (y.mode != Addressing_Constant) {
+				error(y.expr, "Second argument to %.*s must be constant as it is the expected value", LIT(builtin_procs[id].name));
+			}
+
+			if (x.mode == Addressing_Constant) {
+				// NOTE(bill): just completely ignore this intrinsic entirely
+				*operand = x;
+				return true;
+			}
+
+			operand->mode = Addressing_Value;
+			operand->type = x.type;
+		}
+		break;
+
+
+
 
 	case BuiltinProc_type_base_type:
 		if (operand->mode != Addressing_Type) {

+ 13 - 1
src/check_stmt.cpp

@@ -10,7 +10,19 @@ bool is_diverging_stmt(Ast *stmt) {
 		String name = expr->CallExpr.proc->BasicDirective.name;
 		return name == "panic";
 	}
-	Type *t = type_of_expr(expr->CallExpr.proc);
+	Ast *proc = unparen_expr(expr->CallExpr.proc);
+	TypeAndValue tv = proc->tav;
+	if (tv.mode == Addressing_Builtin) {
+		Entity *e = entity_of_node(proc);
+		BuiltinProcId id = BuiltinProc_Invalid;
+		if (e != nullptr) {
+			id = cast(BuiltinProcId)e->Builtin.id;
+		} else {
+			id = BuiltinProc_DIRECTIVE;
+		}
+		return builtin_procs[id].diverging;
+	}
+	Type *t = tv.type;
 	t = base_type(t);
 	return t != nullptr && t->kind == Type_Proc && t->Proc.diverging;
 }

+ 1 - 0
src/checker.hpp

@@ -59,6 +59,7 @@ struct BuiltinProc {
 	bool     variadic;
 	ExprKind kind;
 	BuiltinProcPkg pkg;
+	bool diverging;
 };
 
 

+ 12 - 0
src/checker_builtin_procs.hpp

@@ -41,6 +41,9 @@ enum BuiltinProcId {
 
 	BuiltinProc_alloca,
 	BuiltinProc_cpu_relax,
+	BuiltinProc_trap,
+	BuiltinProc_debug_trap,
+	BuiltinProc_read_cycle_counter,
 
 	BuiltinProc_volatile_store,
 	BuiltinProc_volatile_load,
@@ -122,6 +125,7 @@ enum BuiltinProcId {
 	BuiltinProc_fixed_point_mul_sat,
 	BuiltinProc_fixed_point_div_sat,
 
+	BuiltinProc_expect,
 
 	// Constant type tests
 
@@ -246,6 +250,11 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
 	{STR_LIT("alloca"),    2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
 	{STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
 
+	{STR_LIT("trap"),               0, false, Expr_Stmt, BuiltinProcPkg_intrinsics, /*diverging*/true},
+	{STR_LIT("debug_trap"),         0, false, Expr_Stmt, BuiltinProcPkg_intrinsics, /*diverging*/false},
+	{STR_LIT("read_cycle_counter"), 0, false, Expr_Expr, BuiltinProcPkg_intrinsics},
+
+
 	{STR_LIT("volatile_store"),  2, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
 	{STR_LIT("volatile_load"),   1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
 
@@ -327,6 +336,9 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
 	{STR_LIT("fixed_point_mul_sat"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
 	{STR_LIT("fixed_point_div_sat"), 3, false, Expr_Expr, BuiltinProcPkg_intrinsics},
 
+	{STR_LIT("expect"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics},
+
+
 	{STR_LIT(""), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics},
 	{STR_LIT("type_base_type"),            1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
 	{STR_LIT("type_core_type"),            1, false, Expr_Expr, BuiltinProcPkg_intrinsics},

+ 58 - 0
src/llvm_backend.cpp

@@ -9049,6 +9049,40 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
 		}
 		return {};
 
+
+	case BuiltinProc_debug_trap:
+	case BuiltinProc_trap:
+		{
+			char const *name = nullptr;
+			switch (id) {
+			case BuiltinProc_debug_trap: name = "llvm.debugtrap"; break;
+			case BuiltinProc_trap:       name = "llvm.trap";      break;
+			}
+
+			unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
+			GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
+			LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0);
+
+			LLVMBuildCall(p->builder, ip, nullptr, 0, "");
+			if (id == BuiltinProc_trap) {
+				LLVMBuildUnreachable(p->builder);
+			}
+			return {};
+		}
+
+	case BuiltinProc_read_cycle_counter:
+		{
+			char const *name = "llvm.readcyclecounter";
+			unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
+			GB_ASSERT_MSG(id != 0, "Unable to find %s", name);
+			LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0);
+
+			lbValue res = {};
+			res.value = LLVMBuildCall(p->builder, ip, nullptr, 0, "");
+			res.type = tv.type;
+			return res;
+		}
+
 	case BuiltinProc_atomic_fence:
 		LLVMBuildFence(p->builder, LLVMAtomicOrderingSequentiallyConsistent, false, "");
 		return {};
@@ -9322,6 +9356,30 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv,
 			res.type = platform_type;
 			return lb_emit_conv(p, res, tv.type);
 		}
+
+	case BuiltinProc_expect:
+		{
+			Type *t = default_type(tv.type);
+			lbValue x = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t);
+			lbValue y = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t);
+
+			char const *name = "llvm.expect";
+
+			LLVMTypeRef types[1] = {lb_type(p->module, t)};
+			unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name));
+			GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0]));
+			LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types));
+
+			lbValue res = {};
+
+			LLVMValueRef args[2] = {};
+			args[0] = x.value;
+			args[1] = y.value;
+
+			res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), "");
+			res.type = t;
+			return lb_emit_conv(p, res, t);
+		}
 	}
 
 	GB_PANIC("Unhandled built-in procedure %.*s", LIT(builtin_procs[id].name));

+ 1 - 0
src/parser.hpp

@@ -339,6 +339,7 @@ AST_KIND(_ExprBegin,  "",  bool) \
 		Token        ellipsis; \
 		ProcInlining inlining; \
 		bool         optional_ok_one; \
+		i32          builtin_id; \
 	}) \
 	AST_KIND(FieldValue,      "field value",              struct { Token eq; Ast *field, *value; }) \
 	AST_KIND(TernaryExpr,     "ternary expression",       struct { Ast *cond, *x, *y; }) \

+ 16 - 2
src/types.cpp

@@ -460,8 +460,8 @@ gb_global Type basic_types[] = {
 	{Type_Basic, {Basic_i64,               BasicFlag_Integer,                          8, STR_LIT("i64")}},
 	{Type_Basic, {Basic_u64,               BasicFlag_Integer | BasicFlag_Unsigned,     8, STR_LIT("u64")}},
 
-	{Type_Basic, {Basic_i128,               BasicFlag_Integer,                        16, STR_LIT("i128")}},
-	{Type_Basic, {Basic_u128,               BasicFlag_Integer | BasicFlag_Unsigned,   16, STR_LIT("u128")}},
+	{Type_Basic, {Basic_i128,              BasicFlag_Integer,                         16, STR_LIT("i128")}},
+	{Type_Basic, {Basic_u128,              BasicFlag_Integer | BasicFlag_Unsigned,    16, STR_LIT("u128")}},
 
 	{Type_Basic, {Basic_rune,              BasicFlag_Integer | BasicFlag_Rune,         4, STR_LIT("rune")}},
 
@@ -1012,6 +1012,20 @@ bool is_type_integer(Type *t) {
 	}
 	return false;
 }
+bool is_type_integer_like(Type *t) {
+	t = core_type(t);
+	if (t->kind == Type_Basic) {
+		return (t->Basic.flags & (BasicFlag_Integer|BasicFlag_Boolean)) != 0;
+	}
+	if (t->kind == Type_BitSet) {
+		if (t->BitSet.underlying) {
+			return is_type_integer_like(t->BitSet.underlying);
+		}
+		return true;
+	}
+	return false;
+}
+
 bool is_type_unsigned(Type *t) {
 	t = base_type(t);
 	// t = core_type(t);