use of io.airlift.bytecode.FieldDefinition in project trino by trinodb.
the class EmbedVersion method implementRunnable.
private static void implementRunnable(ClassDefinition classDefinition) {
FieldDefinition field = classDefinition.declareField(a(PRIVATE), "runnable", Runnable.class);
Parameter parameter = arg("runnable", type(Runnable.class));
MethodDefinition constructor = classDefinition.declareConstructor(a(PUBLIC), parameter);
constructor.getBody().comment("super();").append(constructor.getThis()).invokeConstructor(Object.class).append(constructor.getThis()).append(parameter).putField(field).ret();
MethodDefinition run = classDefinition.declareMethod(a(PUBLIC), "run", type(void.class));
run.getBody().comment("runnable.run();").append(run.getThis()).getField(field).invokeInterface(Runnable.class, "run", void.class).ret();
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class AccumulatorCompiler method generateConstructor.
private static void generateConstructor(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, FieldDefinition inputChannelsField, FieldDefinition maskChannelField, List<FieldDefinition> lambdaProviderFields, boolean grouped) {
Parameter stateDescriptors = arg("stateDescriptors", type(List.class, AccumulatorStateDescriptor.class));
Parameter inputChannels = arg("inputChannels", type(List.class, Integer.class));
Parameter maskChannel = arg("maskChannel", type(Optional.class, Integer.class));
Parameter lambdaProviders = arg("lambdaProviders", type(List.class, LambdaProvider.class));
MethodDefinition method = definition.declareConstructor(a(PUBLIC), stateDescriptors, inputChannels, maskChannel, lambdaProviders);
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
body.comment("super();").append(thisVariable).invokeConstructor(Object.class);
for (int i = 0; i < stateFieldAndDescriptors.size(); i++) {
body.append(thisVariable.setField(stateFieldAndDescriptors.get(i).getStateSerializerField(), stateDescriptors.invoke("get", Object.class, constantInt(i)).cast(AccumulatorStateDescriptor.class).invoke("getSerializer", AccumulatorStateSerializer.class)));
body.append(thisVariable.setField(stateFieldAndDescriptors.get(i).getStateFactoryField(), stateDescriptors.invoke("get", Object.class, constantInt(i)).cast(AccumulatorStateDescriptor.class).invoke("getFactory", AccumulatorStateFactory.class)));
}
for (int i = 0; i < lambdaProviderFields.size(); i++) {
body.append(thisVariable.setField(lambdaProviderFields.get(i), lambdaProviders.invoke("get", Object.class, constantInt(i)).cast(LambdaProvider.class)));
}
body.append(thisVariable.setField(inputChannelsField, generateRequireNotNull(inputChannels)));
body.append(thisVariable.setField(maskChannelField, generateRequireNotNull(maskChannel)));
String createState;
if (grouped) {
createState = "createGroupedState";
} else {
createState = "createSingleState";
}
for (StateFieldAndDescriptor stateFieldAndDescriptor : stateFieldAndDescriptors) {
FieldDefinition stateField = stateFieldAndDescriptor.getStateField();
BytecodeExpression stateFactory = thisVariable.getField(stateFieldAndDescriptor.getStateFactoryField());
body.append(thisVariable.setField(stateField, stateFactory.invoke(createState, Object.class).cast(stateField.getType())));
}
body.ret();
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class AccumulatorCompiler method generateAddIntermediateAsCombine.
private static void generateAddIntermediateAsCombine(ClassDefinition definition, List<StateFieldAndDescriptor> stateFieldAndDescriptors, List<Class<?>> lambdaInterfaces, List<FieldDefinition> lambdaProviderFields, MethodHandle combineFunction, CallSiteBinder callSiteBinder, boolean grouped) {
MethodDefinition method = declareAddIntermediate(definition, grouped);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
int stateCount = stateFieldAndDescriptors.size();
List<Variable> scratchStates = new ArrayList<>();
for (int i = 0; i < stateCount; i++) {
Class<?> scratchStateClass = stateFieldAndDescriptors.get(i).getStateDescriptor().getFactory().getSingleStateClass();
scratchStates.add(scope.declareVariable(scratchStateClass, "scratchState_" + i));
}
List<Variable> block;
if (stateCount == 1) {
block = ImmutableList.of(scope.getVariable("block"));
} else {
// ColumnarRow is used to get the column blocks represents each state, this allows to
// 1. handle single state and multiple states in a unified way
// 2. avoid the cost of constructing SingleRowBlock for each group
Variable columnarRow = scope.declareVariable(ColumnarRow.class, "columnarRow");
body.append(columnarRow.set(invokeStatic(ColumnarRow.class, "toColumnarRow", ColumnarRow.class, scope.getVariable("block"))));
block = new ArrayList<>();
for (int i = 0; i < stateCount; i++) {
Variable columnBlock = scope.declareVariable(Block.class, "columnBlock_" + i);
body.append(columnBlock.set(columnarRow.invoke("getField", Block.class, constantInt(i))));
block.add(columnBlock);
}
}
Variable position = scope.declareVariable(int.class, "position");
for (int i = 0; i < stateCount; i++) {
FieldDefinition stateFactoryField = stateFieldAndDescriptors.get(i).getStateFactoryField();
body.comment(format("scratchState_%s = stateFactory[%s].createSingleState();", i, i)).append(thisVariable.getField(stateFactoryField)).invokeInterface(AccumulatorStateFactory.class, "createSingleState", Object.class).checkCast(scratchStates.get(i).getType()).putVariable(scratchStates.get(i));
}
List<FieldDefinition> stateFields = stateFieldAndDescriptors.stream().map(StateFieldAndDescriptor::getStateField).collect(toImmutableList());
if (grouped) {
generateEnsureCapacity(scope, stateFields, body);
}
BytecodeBlock loopBody = new BytecodeBlock();
loopBody.comment("combine(state_0, state_1, ... scratchState_0, scratchState_1, ... lambda_0, lambda_1, ...)");
for (FieldDefinition stateField : stateFields) {
if (grouped) {
Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
loopBody.append(thisVariable.getField(stateField).invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
}
loopBody.append(thisVariable.getField(stateField));
}
for (int i = 0; i < stateCount; i++) {
FieldDefinition stateSerializerField = stateFieldAndDescriptors.get(i).getStateSerializerField();
loopBody.append(thisVariable.getField(stateSerializerField).invoke("deserialize", void.class, block.get(i), position, scratchStates.get(i).cast(Object.class)));
loopBody.append(scratchStates.get(i));
}
for (int i = 0; i < lambdaInterfaces.size(); i++) {
loopBody.append(scope.getThis().getField(lambdaProviderFields.get(i)).invoke("getLambda", Object.class).cast(lambdaInterfaces.get(i)));
}
loopBody.append(invoke(callSiteBinder.bind(combineFunction), "combine"));
if (grouped) {
// skip rows with null group id
IfStatement ifStatement = new IfStatement("if (!groupIdsBlock.isNull(position))").condition(not(scope.getVariable("groupIdsBlock").invoke("isNull", boolean.class, position))).ifTrue(loopBody);
loopBody = new BytecodeBlock().append(ifStatement);
}
body.append(generateBlockNonNullPositionForLoop(scope, position, loopBody)).ret();
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class AccumulatorCompiler method generateGroupedEvaluateFinal.
private static void generateGroupedEvaluateFinal(ClassDefinition definition, List<FieldDefinition> stateFields, MethodHandle outputFunction, CallSiteBinder callSiteBinder) {
Parameter groupId = arg("groupId", int.class);
Parameter out = arg("out", BlockBuilder.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC), "evaluateFinal", type(void.class), groupId, out);
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
List<BytecodeExpression> states = new ArrayList<>();
for (FieldDefinition stateField : stateFields) {
BytecodeExpression state = thisVariable.getField(stateField);
body.append(state.invoke("setGroupId", void.class, groupId.cast(long.class)));
states.add(state);
}
body.comment("output(state_0, state_1, ..., out)");
states.forEach(body::append);
body.append(out);
body.append(invoke(callSiteBinder.bind(outputFunction), "output"));
body.ret();
}
use of io.airlift.bytecode.FieldDefinition in project hetu-core by openlookeng.
the class AccumulatorCompiler method generateSetGroupIdFromGroupIdsBlock.
private static void generateSetGroupIdFromGroupIdsBlock(Scope scope, List<FieldDefinition> stateFields, BytecodeBlock block) {
Variable groupIdsBlock = scope.getVariable("groupIdsBlock");
Variable position = scope.getVariable("position");
for (FieldDefinition stateField : stateFields) {
BytecodeExpression state = scope.getThis().getField(stateField);
block.append(state.invoke("setGroupId", void.class, groupIdsBlock.invoke("getGroupId", long.class, position)));
}
}
Aggregations