Skip to content

fix: preserve int semantics for OpenCL work-item getters#200

Open
ItsOtherMauridian wants to merge 1 commit into
Syncleus:masterfrom
ItsOtherMauridian:fix-int-workitem-getters
Open

fix: preserve int semantics for OpenCL work-item getters#200
ItsOtherMauridian wants to merge 1 commit into
Syncleus:masterfrom
ItsOtherMauridian:fix-int-workitem-getters

Conversation

@ItsOtherMauridian
Copy link
Copy Markdown

@ItsOtherMauridian ItsOtherMauridian commented May 12, 2026

Summary

  • Preserve Java int semantics for OpenCL work-item/size getter mappings by emitting an explicit (int) cast before enclosing widening/arithmetic.
  • Exclude getPassId() from that cast because its generated helper already returns int.
  • Lower Java long multiplication (LMUL) to an aparapi_lmul helper implemented with 32-bit OpenCL operations, avoiding driver/device miscompilation of native 64-bit multiply.
  • Emit the long-multiply helper only when the entrypoint or a called method actually contains LMUL.
  • Add regression codegen tests for both relevant cases:
    • (long)getGlobalId() * 100 near Integer.MAX_VALUE to preserve Java getter narrowing semantics.
    • (long)value * 100 to ensure generated OpenCL uses aparapi_lmul(...) instead of native *.
  • Update affected codegen snapshots for the explicit get_global_id narrowing cast.

Why this addresses #38

The issue can be hit through two layers:

  1. Aparapi maps Java int work-item getters to OpenCL functions that return size_t; without narrowing first, enclosing casts/arithmetic can observe the wrong width.
  2. Some OpenCL implementations/devices miscompile native 64-bit integer multiplication; generated kernels should not rely on native long * long for this case.

This PR handles both: it preserves the Java getter return semantics and routes generated LMUL through a helper based on 32-bit operations.

Test Plan

  • docker run --rm -v "$PWD":/workspace -w /workspace maven:3.8.8-eclipse-temurin-8 mvn -q -Dtest=com.aparapi.codegen.test.LongMultiplyCastOverflowTest,com.aparapi.codegen.test.LongCastMultiplyTest test
  • docker run --rm -v "$PWD":/workspace -w /workspace maven:3.8.8-eclipse-temurin-8 mvn -q -Dtest=com.aparapi.codegen.test.CallGetPassIdTest,com.aparapi.codegen.test.LongCastMultiplyTest,com.aparapi.codegen.test.CompositeArbitraryScopeTest,com.aparapi.codegen.test.ObjectArrayMemberAccessTest,com.aparapi.codegen.test.ObjectArrayMemberCallTest,com.aparapi.codegen.test.ObjectArrayMemberGetterSetterTest,com.aparapi.codegen.test.ObjectArrayMemberHierarchyTest,com.aparapi.codegen.test.ObjectRefCopyTest,com.aparapi.SimpleScalaTest test
  • docker run --rm -v "$PWD":/workspace -w /workspace maven:3.8.8-eclipse-temurin-8 mvn -q -Dtest=com.aparapi.codegen.test.*Test,com.aparapi.SimpleScalaTest test

/claim #38

@ItsOtherMauridian ItsOtherMauridian force-pushed the fix-int-workitem-getters branch from 5fb5084 to 026f2e8 Compare May 13, 2026 14:33
@ItsOtherMauridian
Copy link
Copy Markdown
Author

Small clarification after reviewing the related closed PR #205: this PR now covers both parts of the failure mode for #38.

  • It preserves Java int semantics for Aparapi work-item/size getters before widening/arithmetic.
  • It avoids native OpenCL 64-bit multiply for Java LMUL by emitting aparapi_lmul(...) backed by 32-bit operations.

I also re-ran the targeted regression checks in Docker:

docker run --rm -v "$PWD":/workspace -w /workspace maven:3.8.8-eclipse-temurin-8 \
  mvn -q -Dtest=com.aparapi.codegen.test.LongMultiplyCastOverflowTest,com.aparapi.codegen.test.LongCastMultiplyTest test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant