From 0951942bba25f85ad29a4f096ed51a356652b5a2 Mon Sep 17 00:00:00 2001
From: Thomas Munro <tmunro@postgresql.org>
Date: Wed, 17 Sep 2025 12:00:16 +1200
Subject: [PATCH] jit: Fix type used for Datum values in LLVM IR.

Commit 2a600a93 made Datum 8 bytes wide everywhere.  It was no longer
appropriate to use TypeSizeT on 32 bit systems, and JIT compilation
would fail with various type check errors.  Introduce a separate
LLVMTypeRef with the name TypeDatum.  TypeSizeT is still used in some
places for actual size_t values.

Reported-by: Dmitry Mityugov <d.mityugov@postgrespro.ru>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Tested-by: Dmitry Mityugov <d.mityugov@postgrespro.ru>
Discussion: https://postgr.es/m/0a9f0be59171c2e8f1b3bc10f4fcf267%40postgrespro.ru
---
 src/backend/jit/llvm/llvmjit.c        |   2 +
 src/backend/jit/llvm/llvmjit_deform.c |  10 +-
 src/backend/jit/llvm/llvmjit_expr.c   | 150 +++++++++++++-------------
 src/backend/jit/llvm/llvmjit_types.c  |   1 +
 src/include/jit/llvmjit.h             |   1 +
 src/include/jit/llvmjit_emit.h        |  11 +-
 6 files changed, 94 insertions(+), 81 deletions(-)

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index 46511624f0..e978b996ba 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -54,6 +54,7 @@ typedef struct LLVMJitHandle
 
 /* types & functions commonly needed for JITing */
 LLVMTypeRef TypeSizeT;
+LLVMTypeRef TypeDatum;
 LLVMTypeRef TypeParamBool;
 LLVMTypeRef TypeStorageBool;
 LLVMTypeRef TypePGFunction;
@@ -1011,6 +1012,7 @@ llvm_create_types(void)
 	LLVMDisposeMemoryBuffer(buf);
 
 	TypeSizeT = llvm_pg_var_type("TypeSizeT");
+	TypeDatum = llvm_pg_var_type("TypeDatum");
 	TypeParamBool = load_return_type(llvm_types_module, "FunctionReturningBool");
 	TypeStorageBool = llvm_pg_var_type("TypeStorageBool");
 	TypePGFunction = llvm_pg_var_type("TypePGFunction");
diff --git a/src/backend/jit/llvm/llvmjit_deform.c b/src/backend/jit/llvm/llvmjit_deform.c
index c562edd094..9791073faf 100644
--- a/src/backend/jit/llvm/llvmjit_deform.c
+++ b/src/backend/jit/llvm/llvmjit_deform.c
@@ -479,8 +479,8 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
 						   l_gep(b, LLVMInt8TypeInContext(lc), v_tts_nulls, &l_attno, 1, ""));
 			/* store zero datum */
 			LLVMBuildStore(b,
-						   l_sizet_const(0),
-						   l_gep(b, TypeSizeT, v_tts_values, &l_attno, 1, ""));
+						   l_datum_const(0),
+						   l_gep(b, TypeDatum, v_tts_values, &l_attno, 1, ""));
 
 			LLVMBuildBr(b, b_next);
 			attguaranteedalign = false;
@@ -644,7 +644,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
 		}
 
 		/* compute address to store value at */
-		v_resultp = l_gep(b, TypeSizeT, v_tts_values, &l_attno, 1, "");
+		v_resultp = l_gep(b, TypeDatum, v_tts_values, &l_attno, 1, "");
 
 		/* store null-byte (false) */
 		LLVMBuildStore(b, l_int8_const(lc, 0),
@@ -663,7 +663,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
 			v_tmp_loaddata =
 				LLVMBuildPointerCast(b, v_attdatap, vartypep, "");
 			v_tmp_loaddata = l_load(b, vartype, v_tmp_loaddata, "attr_byval");
-			v_tmp_loaddata = LLVMBuildZExt(b, v_tmp_loaddata, TypeSizeT, "");
+			v_tmp_loaddata = LLVMBuildZExt(b, v_tmp_loaddata, TypeDatum, "");
 
 			LLVMBuildStore(b, v_tmp_loaddata, v_resultp);
 		}
@@ -675,7 +675,7 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc,
 			v_tmp_loaddata =
 				LLVMBuildPtrToInt(b,
 								  v_attdatap,
-								  TypeSizeT,
+								  TypeDatum,
 								  "attr_ptr");
 			LLVMBuildStore(b, v_tmp_loaddata, v_resultp);
 		}
diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c
index 890bcb0b0a..712b35df7e 100644
--- a/src/backend/jit/llvm/llvmjit_expr.c
+++ b/src/backend/jit/llvm/llvmjit_expr.c
@@ -316,7 +316,7 @@ llvm_compile_expr(ExprState *state)
 		op = &state->steps[opno];
 		opcode = ExecEvalStepOp(state, op);
 
-		v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));
+		v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeDatum));
 		v_resnullp = l_ptr_const(op->resnull, l_ptr(TypeStorageBool));
 
 		switch (opcode)
@@ -326,7 +326,7 @@ llvm_compile_expr(ExprState *state)
 					LLVMValueRef v_tmpisnull;
 					LLVMValueRef v_tmpvalue;
 
-					v_tmpvalue = l_load(b, TypeSizeT, v_tmpvaluep, "");
+					v_tmpvalue = l_load(b, TypeDatum, v_tmpvaluep, "");
 					v_tmpisnull = l_load(b, TypeStorageBool, v_tmpisnullp, "");
 
 					LLVMBuildStore(b, v_tmpisnull, v_isnullp);
@@ -336,7 +336,7 @@ llvm_compile_expr(ExprState *state)
 				}
 
 			case EEOP_DONE_NO_RETURN:
-				LLVMBuildRet(b, l_sizet_const(0));
+				LLVMBuildRet(b, l_datum_const(0));
 				break;
 
 			case EEOP_INNER_FETCHSOME:
@@ -478,7 +478,7 @@ llvm_compile_expr(ExprState *state)
 					}
 
 					v_attnum = l_int32_const(lc, op->d.var.attnum);
-					value = l_load_gep1(b, TypeSizeT, v_values, v_attnum, "");
+					value = l_load_gep1(b, TypeDatum, v_values, v_attnum, "");
 					isnull = l_load_gep1(b, TypeStorageBool, v_nulls, v_attnum, "");
 					LLVMBuildStore(b, value, v_resvaluep);
 					LLVMBuildStore(b, isnull, v_resnullp);
@@ -562,13 +562,13 @@ llvm_compile_expr(ExprState *state)
 
 					/* load data */
 					v_attnum = l_int32_const(lc, op->d.assign_var.attnum);
-					v_value = l_load_gep1(b, TypeSizeT, v_values, v_attnum, "");
+					v_value = l_load_gep1(b, TypeDatum, v_values, v_attnum, "");
 					v_isnull = l_load_gep1(b, TypeStorageBool, v_nulls, v_attnum, "");
 
 					/* compute addresses of targets */
 					v_resultnum = l_int32_const(lc, op->d.assign_var.resultnum);
 					v_rvaluep = l_gep(b,
-									  TypeSizeT,
+									  TypeDatum,
 									  v_resultvalues,
 									  &v_resultnum, 1, "");
 					v_risnullp = l_gep(b,
@@ -595,13 +595,13 @@ llvm_compile_expr(ExprState *state)
 					size_t		resultnum = op->d.assign_tmp.resultnum;
 
 					/* load data */
-					v_value = l_load(b, TypeSizeT, v_tmpvaluep, "");
+					v_value = l_load(b, TypeDatum, v_tmpvaluep, "");
 					v_isnull = l_load(b, TypeStorageBool, v_tmpisnullp, "");
 
 					/* compute addresses of targets */
 					v_resultnum = l_int32_const(lc, resultnum);
 					v_rvaluep =
-						l_gep(b, TypeSizeT, v_resultvalues, &v_resultnum, 1, "");
+						l_gep(b, TypeDatum, v_resultvalues, &v_resultnum, 1, "");
 					v_risnullp =
 						l_gep(b, TypeStorageBool, v_resultnulls, &v_resultnum, 1, "");
 
@@ -650,7 +650,7 @@ llvm_compile_expr(ExprState *state)
 					LLVMValueRef v_constvalue,
 								v_constnull;
 
-					v_constvalue = l_sizet_const(op->d.constval.value);
+					v_constvalue = l_datum_const(op->d.constval.value);
 					v_constnull = l_sbool_const(op->d.constval.isnull);
 
 					LLVMBuildStore(b, v_constvalue, v_resvaluep);
@@ -798,7 +798,7 @@ llvm_compile_expr(ExprState *state)
 						LLVMBuildStore(b, l_sbool_const(0), v_boolanynullp);
 
 					v_boolnull = l_load(b, TypeStorageBool, v_resnullp, "");
-					v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_boolvalue = l_load(b, TypeDatum, v_resvaluep, "");
 
 					/* check if current input is NULL */
 					LLVMBuildCondBr(b,
@@ -818,7 +818,7 @@ llvm_compile_expr(ExprState *state)
 					LLVMPositionBuilderAtEnd(b, b_boolcheckfalse);
 					LLVMBuildCondBr(b,
 									LLVMBuildICmp(b, LLVMIntEQ, v_boolvalue,
-												  l_sizet_const(0), ""),
+												  l_datum_const(0), ""),
 									b_boolisfalse,
 									b_boolcont);
 
@@ -846,7 +846,7 @@ llvm_compile_expr(ExprState *state)
 					/* set resnull to true */
 					LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
 					/* reset resvalue */
-					LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+					LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 
 					LLVMBuildBr(b, opblocks[opno + 1]);
 					break;
@@ -889,7 +889,7 @@ llvm_compile_expr(ExprState *state)
 					if (opcode == EEOP_BOOL_OR_STEP_FIRST)
 						LLVMBuildStore(b, l_sbool_const(0), v_boolanynullp);
 					v_boolnull = l_load(b, TypeStorageBool, v_resnullp, "");
-					v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_boolvalue = l_load(b, TypeDatum, v_resvaluep, "");
 
 					LLVMBuildCondBr(b,
 									LLVMBuildICmp(b, LLVMIntEQ, v_boolnull,
@@ -908,7 +908,7 @@ llvm_compile_expr(ExprState *state)
 					LLVMPositionBuilderAtEnd(b, b_boolchecktrue);
 					LLVMBuildCondBr(b,
 									LLVMBuildICmp(b, LLVMIntEQ, v_boolvalue,
-												  l_sizet_const(1), ""),
+												  l_datum_const(1), ""),
 									b_boolistrue,
 									b_boolcont);
 
@@ -936,7 +936,7 @@ llvm_compile_expr(ExprState *state)
 					/* set resnull to true */
 					LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
 					/* reset resvalue */
-					LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+					LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 
 					LLVMBuildBr(b, opblocks[opno + 1]);
 					break;
@@ -948,13 +948,13 @@ llvm_compile_expr(ExprState *state)
 					LLVMValueRef v_negbool;
 
 					/* compute !boolvalue */
-					v_boolvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_boolvalue = l_load(b, TypeDatum, v_resvaluep, "");
 					v_negbool = LLVMBuildZExt(b,
 											  LLVMBuildICmp(b, LLVMIntEQ,
 															v_boolvalue,
-															l_sizet_const(0),
+															l_datum_const(0),
 															""),
-											  TypeSizeT, "");
+											  TypeDatum, "");
 
 					/*
 					 * Store it back in resvalue.  We can ignore resnull here;
@@ -977,7 +977,7 @@ llvm_compile_expr(ExprState *state)
 					b_qualfail = l_bb_before_v(opblocks[opno + 1],
 											   "op.%d.qualfail", opno);
 
-					v_resvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_resvalue = l_load(b, TypeDatum, v_resvaluep, "");
 					v_resnull = l_load(b, TypeStorageBool, v_resnullp, "");
 
 					v_nullorfalse =
@@ -985,7 +985,7 @@ llvm_compile_expr(ExprState *state)
 									LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
 												  l_sbool_const(1), ""),
 									LLVMBuildICmp(b, LLVMIntEQ, v_resvalue,
-												  l_sizet_const(0), ""),
+												  l_datum_const(0), ""),
 									"");
 
 					LLVMBuildCondBr(b,
@@ -998,7 +998,7 @@ llvm_compile_expr(ExprState *state)
 					/* set resnull to false */
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
 					/* set resvalue to false */
-					LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+					LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 					/* and jump out */
 					LLVMBuildBr(b, opblocks[op->d.qualexpr.jumpdone]);
 					break;
@@ -1051,7 +1051,7 @@ llvm_compile_expr(ExprState *state)
 
 					/* Transfer control if current result is null or false */
 
-					v_resvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_resvalue = l_load(b, TypeDatum, v_resvaluep, "");
 					v_resnull = l_load(b, TypeStorageBool, v_resnullp, "");
 
 					v_nullorfalse =
@@ -1059,7 +1059,7 @@ llvm_compile_expr(ExprState *state)
 									LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
 												  l_sbool_const(1), ""),
 									LLVMBuildICmp(b, LLVMIntEQ, v_resvalue,
-												  l_sizet_const(0), ""),
+												  l_datum_const(0), ""),
 									"");
 
 					LLVMBuildCondBr(b,
@@ -1078,8 +1078,8 @@ llvm_compile_expr(ExprState *state)
 						LLVMBuildSelect(b,
 										LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
 													  l_sbool_const(1), ""),
-										l_sizet_const(1),
-										l_sizet_const(0),
+										l_datum_const(1),
+										l_datum_const(0),
 										"");
 					LLVMBuildStore(b, v_resvalue, v_resvaluep);
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
@@ -1097,8 +1097,8 @@ llvm_compile_expr(ExprState *state)
 						LLVMBuildSelect(b,
 										LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
 													  l_sbool_const(1), ""),
-										l_sizet_const(0),
-										l_sizet_const(1),
+										l_datum_const(0),
+										l_datum_const(1),
 										"");
 					LLVMBuildStore(b, v_resvalue, v_resvaluep);
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
@@ -1148,11 +1148,11 @@ llvm_compile_expr(ExprState *state)
 					if (opcode == EEOP_BOOLTEST_IS_TRUE ||
 						opcode == EEOP_BOOLTEST_IS_FALSE)
 					{
-						LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 					}
 					else
 					{
-						LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(1), v_resvaluep);
 					}
 
 					LLVMBuildBr(b, opblocks[opno + 1]);
@@ -1170,14 +1170,14 @@ llvm_compile_expr(ExprState *state)
 					else
 					{
 						LLVMValueRef v_value =
-							l_load(b, TypeSizeT, v_resvaluep, "");
+							l_load(b, TypeDatum, v_resvaluep, "");
 
 						v_value = LLVMBuildZExt(b,
 												LLVMBuildICmp(b, LLVMIntEQ,
 															  v_value,
-															  l_sizet_const(0),
+															  l_datum_const(0),
 															  ""),
-												TypeSizeT, "");
+												TypeDatum, "");
 						LLVMBuildStore(b, v_value, v_resvaluep);
 					}
 					LLVMBuildBr(b, opblocks[opno + 1]);
@@ -1279,11 +1279,11 @@ llvm_compile_expr(ExprState *state)
 								v_casenull;
 
 					v_casevaluep = l_ptr_const(op->d.casetest.value,
-											   l_ptr(TypeSizeT));
+											   l_ptr(TypeDatum));
 					v_casenullp = l_ptr_const(op->d.casetest.isnull,
 											  l_ptr(TypeStorageBool));
 
-					v_casevalue = l_load(b, TypeSizeT, v_casevaluep, "");
+					v_casevalue = l_load(b, TypeDatum, v_casevaluep, "");
 					v_casenull = l_load(b, TypeStorageBool, v_casenullp, "");
 					LLVMBuildStore(b, v_casevalue, v_resvaluep);
 					LLVMBuildStore(b, v_casenull, v_resnullp);
@@ -1345,9 +1345,9 @@ llvm_compile_expr(ExprState *state)
 					LLVMPositionBuilderAtEnd(b, b_notnull);
 
 					v_valuep = l_ptr_const(op->d.make_readonly.value,
-										   l_ptr(TypeSizeT));
+										   l_ptr(TypeDatum));
 
-					v_value = l_load(b, TypeSizeT, v_valuep, "");
+					v_value = l_load(b, TypeDatum, v_valuep, "");
 
 					v_params[0] = v_value;
 					v_ret =
@@ -1415,11 +1415,11 @@ llvm_compile_expr(ExprState *state)
 									b_calloutput);
 
 					LLVMPositionBuilderAtEnd(b, b_skipoutput);
-					v_output_skip = l_sizet_const(0);
+					v_output_skip = l_datum_const(0);
 					LLVMBuildBr(b, b_input);
 
 					LLVMPositionBuilderAtEnd(b, b_calloutput);
-					v_resvalue = l_load(b, TypeSizeT, v_resvaluep, "");
+					v_resvalue = l_load(b, TypeDatum, v_resvaluep, "");
 
 					/* set arg[0] */
 					LLVMBuildStore(b,
@@ -1449,7 +1449,7 @@ llvm_compile_expr(ExprState *state)
 						incoming_values[1] = v_output;
 						incoming_blocks[1] = b_calloutput;
 
-						v_output = LLVMBuildPhi(b, TypeSizeT, "output");
+						v_output = LLVMBuildPhi(b, TypeDatum, "output");
 						LLVMAddIncoming(v_output,
 										incoming_values, incoming_blocks,
 										lengthof(incoming_blocks));
@@ -1463,7 +1463,7 @@ llvm_compile_expr(ExprState *state)
 					{
 						LLVMBuildCondBr(b,
 										LLVMBuildICmp(b, LLVMIntEQ, v_output,
-													  l_sizet_const(0), ""),
+													  l_datum_const(0), ""),
 										opblocks[opno + 1],
 										b_inputcall);
 					}
@@ -1564,9 +1564,9 @@ llvm_compile_expr(ExprState *state)
 					LLVMPositionBuilderAtEnd(b, b_bothargnull);
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
 					if (opcode == EEOP_NOT_DISTINCT)
-						LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(1), v_resvaluep);
 					else
-						LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 
 					LLVMBuildBr(b, opblocks[opno + 1]);
 
@@ -1574,9 +1574,9 @@ llvm_compile_expr(ExprState *state)
 					LLVMPositionBuilderAtEnd(b, b_anyargnull);
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
 					if (opcode == EEOP_NOT_DISTINCT)
-						LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 					else
-						LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(1), v_resvaluep);
 					LLVMBuildBr(b, opblocks[opno + 1]);
 
 					/* neither argument is null: compare */
@@ -1592,8 +1592,8 @@ llvm_compile_expr(ExprState *state)
 							LLVMBuildZExt(b,
 										  LLVMBuildICmp(b, LLVMIntEQ,
 														v_result,
-														l_sizet_const(0), ""),
-										  TypeSizeT, "");
+														l_datum_const(0), ""),
+										  TypeDatum, "");
 					}
 
 					LLVMBuildStore(b, v_fcinfo_isnull, v_resnullp);
@@ -1691,7 +1691,7 @@ llvm_compile_expr(ExprState *state)
 															 ""),
 											   LLVMBuildICmp(b, LLVMIntEQ,
 															 v_retval,
-															 l_sizet_const(1),
+															 l_datum_const(1),
 															 ""),
 											   "");
 					LLVMBuildCondBr(b, v_argsequal, b_argsequal, b_hasnull);
@@ -1699,7 +1699,7 @@ llvm_compile_expr(ExprState *state)
 					/* build block setting result to NULL, if args are equal */
 					LLVMPositionBuilderAtEnd(b, b_argsequal);
 					LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
-					LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+					LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 
 					LLVMBuildBr(b, opblocks[opno + 1]);
 					break;
@@ -1755,7 +1755,7 @@ llvm_compile_expr(ExprState *state)
 
 					LLVMPositionBuilderAtEnd(b, b_isnull);
 
-					LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+					LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 					LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
 
 					LLVMBuildBr(b, opblocks[op->d.returningexpr.jumpdone]);
@@ -1861,7 +1861,7 @@ llvm_compile_expr(ExprState *state)
 									LLVMBuildICmp(b,
 												  LLVMIntEQ,
 												  v_retval,
-												  l_sizet_const(0), ""),
+												  l_datum_const(0), ""),
 									opblocks[opno + 1],
 									opblocks[op->d.rowcompare_step.jumpdone]);
 
@@ -1891,7 +1891,7 @@ llvm_compile_expr(ExprState *state)
 					 */
 					v_cmpresult =
 						LLVMBuildTrunc(b,
-									   l_load(b, TypeSizeT, v_resvaluep, ""),
+									   l_load(b, TypeDatum, v_resvaluep, ""),
 									   LLVMInt32TypeInContext(lc), "");
 
 					switch (cmptype)
@@ -1920,7 +1920,7 @@ llvm_compile_expr(ExprState *state)
 											 v_cmpresult,
 											 l_int32_const(lc, 0),
 											 "");
-					v_result = LLVMBuildZExt(b, v_result, TypeSizeT, "");
+					v_result = LLVMBuildZExt(b, v_result, TypeDatum, "");
 
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
 					LLVMBuildStore(b, v_result, v_resvaluep);
@@ -1961,11 +1961,11 @@ llvm_compile_expr(ExprState *state)
 								v_casenull;
 
 					v_casevaluep = l_ptr_const(op->d.casetest.value,
-											   l_ptr(TypeSizeT));
+											   l_ptr(TypeDatum));
 					v_casenullp = l_ptr_const(op->d.casetest.isnull,
 											  l_ptr(TypeStorageBool));
 
-					v_casevalue = l_load(b, TypeSizeT, v_casevaluep, "");
+					v_casevalue = l_load(b, TypeDatum, v_casevaluep, "");
 					v_casenull = l_load(b, TypeStorageBool, v_casenullp, "");
 					LLVMBuildStore(b, v_casevalue, v_resvaluep);
 					LLVMBuildStore(b, v_casenull, v_resnullp);
@@ -2014,7 +2014,7 @@ llvm_compile_expr(ExprState *state)
 				{
 					LLVMValueRef v_initvalue;
 
-					v_initvalue = l_sizet_const(op->d.hashdatum_initvalue.init_value);
+					v_initvalue = l_datum_const(op->d.hashdatum_initvalue.init_value);
 
 					LLVMBuildStore(b, v_initvalue, v_resvaluep);
 					LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
@@ -2053,24 +2053,24 @@ llvm_compile_expr(ExprState *state)
 						LLVMValueRef tmp;
 
 						tmp = l_ptr_const(&op->d.hashdatum.iresult->value,
-										  l_ptr(TypeSizeT));
+										  l_ptr(TypeDatum));
 
 						/*
 						 * Fetch the previously hashed value from where the
 						 * previous hash operation stored it.
 						 */
-						v_prevhash = l_load(b, TypeSizeT, tmp, "prevhash");
+						v_prevhash = l_load(b, TypeDatum, tmp, "prevhash");
 
 						/*
 						 * Rotate bits left by 1 bit.  Be careful not to
-						 * overflow uint32 when working with size_t.
+						 * overflow uint32 when working with Datum.
 						 */
-						v_tmp1 = LLVMBuildShl(b, v_prevhash, l_sizet_const(1),
+						v_tmp1 = LLVMBuildShl(b, v_prevhash, l_datum_const(1),
 											  "");
 						v_tmp1 = LLVMBuildAnd(b, v_tmp1,
-											  l_sizet_const(0xffffffff), "");
+											  l_datum_const(0xffffffff), "");
 						v_tmp2 = LLVMBuildLShr(b, v_prevhash,
-											   l_sizet_const(31), "");
+											   l_datum_const(31), "");
 						v_prevhash = LLVMBuildOr(b, v_tmp1, v_tmp2,
 												 "rotatedhash");
 					}
@@ -2113,7 +2113,7 @@ llvm_compile_expr(ExprState *state)
 						 * the NULL result and goto jumpdone.
 						 */
 						LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
-						LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+						LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 						LLVMBuildBr(b, opblocks[op->d.hashdatum.jumpdone]);
 					}
 					else
@@ -2145,7 +2145,7 @@ llvm_compile_expr(ExprState *state)
 							 * Store a zero Datum when the Datum to hash is
 							 * NULL
 							 */
-							LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
+							LLVMBuildStore(b, l_datum_const(0), v_resvaluep);
 						}
 
 						LLVMBuildBr(b, opblocks[opno + 1]);
@@ -2178,24 +2178,24 @@ llvm_compile_expr(ExprState *state)
 						LLVMValueRef tmp;
 
 						tmp = l_ptr_const(&op->d.hashdatum.iresult->value,
-										  l_ptr(TypeSizeT));
+										  l_ptr(TypeDatum));
 
 						/*
 						 * Fetch the previously hashed value from where the
 						 * previous hash operation stored it.
 						 */
-						v_prevhash = l_load(b, TypeSizeT, tmp, "prevhash");
+						v_prevhash = l_load(b, TypeDatum, tmp, "prevhash");
 
 						/*
 						 * Rotate bits left by 1 bit.  Be careful not to
-						 * overflow uint32 when working with size_t.
+						 * overflow uint32 when working with Datum.
 						 */
-						v_tmp1 = LLVMBuildShl(b, v_prevhash, l_sizet_const(1),
+						v_tmp1 = LLVMBuildShl(b, v_prevhash, l_datum_const(1),
 											  "");
 						v_tmp1 = LLVMBuildAnd(b, v_tmp1,
-											  l_sizet_const(0xffffffff), "");
+											  l_datum_const(0xffffffff), "");
 						v_tmp2 = LLVMBuildLShr(b, v_prevhash,
-											   l_sizet_const(31), "");
+											   l_datum_const(31), "");
 						v_prevhash = LLVMBuildOr(b, v_tmp1, v_tmp2,
 												 "rotatedhash");
 					}
@@ -2373,7 +2373,7 @@ llvm_compile_expr(ExprState *state)
 					v_aggno = l_int32_const(lc, op->d.aggref.aggno);
 
 					/* load agg value / null */
-					value = l_load_gep1(b, TypeSizeT, v_aggvalues, v_aggno, "aggvalue");
+					value = l_load_gep1(b, TypeDatum, v_aggvalues, v_aggno, "aggvalue");
 					isnull = l_load_gep1(b, TypeStorageBool, v_aggnulls, v_aggno, "aggnull");
 
 					/* and store result */
@@ -2408,7 +2408,7 @@ llvm_compile_expr(ExprState *state)
 					v_wfuncno = l_load(b, LLVMInt32TypeInContext(lc), v_wfuncnop, "v_wfuncno");
 
 					/* load window func value / null */
-					value = l_load_gep1(b, TypeSizeT, v_aggvalues, v_wfuncno,
+					value = l_load_gep1(b, TypeDatum, v_aggvalues, v_wfuncno,
 										"windowvalue");
 					isnull = l_load_gep1(b, TypeStorageBool, v_aggnulls, v_wfuncno,
 										 "windownull");
@@ -2585,8 +2585,8 @@ llvm_compile_expr(ExprState *state)
 
 					LLVMBuildCondBr(b,
 									LLVMBuildICmp(b, LLVMIntEQ,
-												  LLVMBuildPtrToInt(b, v_pergroup_allaggs, TypeSizeT, ""),
-												  l_sizet_const(0), ""),
+												  LLVMBuildPtrToInt(b, v_pergroup_allaggs, TypeDatum, ""),
+												  l_datum_const(0), ""),
 									opblocks[jumpnull],
 									opblocks[opno + 1]);
 					break;
@@ -2788,7 +2788,7 @@ llvm_compile_expr(ExprState *state)
 									 "transnullp");
 					LLVMBuildStore(b,
 								   l_load(b,
-										  TypeSizeT,
+										  TypeDatum,
 										  v_transvaluep,
 										  "transvalue"),
 								   l_funcvaluep(b, v_fcinfo, 0));
@@ -2826,7 +2826,7 @@ llvm_compile_expr(ExprState *state)
 						b_nocall = l_bb_before_v(opblocks[opno + 1],
 												 "op.%d.transnocall", opno);
 
-						v_transvalue = l_load(b, TypeSizeT, v_transvaluep, "");
+						v_transvalue = l_load(b, TypeDatum, v_transvaluep, "");
 						v_transnull = l_load(b, TypeStorageBool, v_transnullp, "");
 
 						/*
diff --git a/src/backend/jit/llvm/llvmjit_types.c b/src/backend/jit/llvm/llvmjit_types.c
index dbe0282e98..167cd554b9 100644
--- a/src/backend/jit/llvm/llvmjit_types.c
+++ b/src/backend/jit/llvm/llvmjit_types.c
@@ -47,6 +47,7 @@
  */
 PGFunction	TypePGFunction;
 size_t		TypeSizeT;
+Datum		TypeDatum;
 bool		TypeStorageBool;
 
 ExecEvalSubroutine TypeExecEvalSubroutine;
diff --git a/src/include/jit/llvmjit.h b/src/include/jit/llvmjit.h
index 5038cf33e3..b3c75022f5 100644
--- a/src/include/jit/llvmjit.h
+++ b/src/include/jit/llvmjit.h
@@ -74,6 +74,7 @@ typedef struct LLVMJitContext
 extern PGDLLIMPORT LLVMTypeRef TypeParamBool;
 extern PGDLLIMPORT LLVMTypeRef TypePGFunction;
 extern PGDLLIMPORT LLVMTypeRef TypeSizeT;
+extern PGDLLIMPORT LLVMTypeRef TypeDatum;
 extern PGDLLIMPORT LLVMTypeRef TypeStorageBool;
 
 extern PGDLLIMPORT LLVMTypeRef StructNullableDatum;
diff --git a/src/include/jit/llvmjit_emit.h b/src/include/jit/llvmjit_emit.h
index df5a9fc850..0e57a332b6 100644
--- a/src/include/jit/llvmjit_emit.h
+++ b/src/include/jit/llvmjit_emit.h
@@ -86,6 +86,15 @@ l_sizet_const(size_t i)
 	return LLVMConstInt(TypeSizeT, i, false);
 }
 
+/*
+ * Emit constant integer.
+ */
+static inline LLVMValueRef
+l_datum_const(Datum i)
+{
+	return LLVMConstInt(TypeDatum, i, false);
+}
+
 /*
  * Emit constant boolean, as used for storage (e.g. global vars, structs).
  */
@@ -313,7 +322,7 @@ l_funcnull(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
 static inline LLVMValueRef
 l_funcvalue(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
 {
-	return l_load(b, TypeSizeT, l_funcvaluep(b, v_fcinfo, argno), "");
+	return l_load(b, TypeDatum, l_funcvaluep(b, v_fcinfo, argno), "");
 }
 
 #endif							/* USE_LLVM */
-- 
2.39.5

