[Mesa-dev] [PATCH 78/92] ac/nir: implement nir_op_{b2i,i2b}

Nicolai Hähnle nhaehnle at gmail.com
Mon Jun 26 14:19:07 UTC 2017


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

Booleans in NIR are ~0 for true, b2i returns 0/1.
---
 src/amd/common/ac_nir_to_llvm.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/amd/common/ac_nir_to_llvm.c b/src/amd/common/ac_nir_to_llvm.c
index e3d1529..0a5f1b3 100644
--- a/src/amd/common/ac_nir_to_llvm.c
+++ b/src/amd/common/ac_nir_to_llvm.c
@@ -1300,20 +1300,34 @@ static LLVMValueRef emit_uint_carry(struct ac_llvm_context *ctx,
 	res = LLVMBuildZExt(ctx->builder, res, ctx->i32, "");
 	return res;
 }
 
 static LLVMValueRef emit_b2f(struct ac_llvm_context *ctx,
 			     LLVMValueRef src0)
 {
 	return LLVMBuildAnd(ctx->builder, src0, LLVMBuildBitCast(ctx->builder, LLVMConstReal(ctx->f32, 1.0), ctx->i32, ""), "");
 }
 
+static LLVMValueRef emit_b2i(struct ac_llvm_context *ctx,
+			     LLVMValueRef src0)
+{
+	return LLVMBuildAnd(ctx->builder, src0, ctx->i32_1, "");
+}
+
+static LLVMValueRef emit_i2b(struct ac_llvm_context *ctx,
+			     LLVMValueRef src0)
+{
+	return LLVMBuildSExt(ctx->builder,
+			     LLVMBuildICmp(ctx->builder, LLVMIntNE, src0, ctx->i32_0, ""),
+			     ctx->i32, "");
+}
+
 static LLVMValueRef emit_f2f16(struct nir_to_llvm_context *ctx,
 			       LLVMValueRef src0)
 {
 	LLVMValueRef result;
 	LLVMValueRef cond;
 
 	src0 = to_float(&ctx->ac, src0);
 	result = LLVMBuildFPTrunc(ctx->builder, src0, ctx->f16, "");
 
 	/* TODO SI/CIK options here */
@@ -1822,20 +1836,26 @@ static void visit_alu(struct ac_nir_context *ctx, const nir_alu_instr *instr)
 		break;
 	case nir_op_uadd_carry:
 		result = emit_uint_carry(&ctx->ac, "llvm.uadd.with.overflow.i32", src[0], src[1]);
 		break;
 	case nir_op_usub_borrow:
 		result = emit_uint_carry(&ctx->ac, "llvm.usub.with.overflow.i32", src[0], src[1]);
 		break;
 	case nir_op_b2f:
 		result = emit_b2f(&ctx->ac, src[0]);
 		break;
+	case nir_op_b2i:
+		result = emit_b2i(&ctx->ac, src[0]);
+		break;
+	case nir_op_i2b:
+		result = emit_i2b(&ctx->ac, src[0]);
+		break;
 	case nir_op_fquantize2f16:
 		result = emit_f2f16(ctx->nctx, src[0]);
 		break;
 	case nir_op_umul_high:
 		result = emit_umul_high(&ctx->ac, src[0], src[1]);
 		break;
 	case nir_op_imul_high:
 		result = emit_imul_high(&ctx->ac, src[0], src[1]);
 		break;
 	case nir_op_pack_half_2x16:
-- 
2.9.3



More information about the mesa-dev mailing list