device function can't do for loops?

__device__ Color trace(Ray &ray, Object *objects, Light *lights, int depth)
{
	float hit = INFINITY;
	float closest = INFINITY;
	Object objectHit;

	return Color(0, 1, 1);
	for (int i = 0; i < OBJECTNUMBER; i++)
	{
		if (objects[i].intersect(ray, hit))
		{
			if (hit < closest)
			{
				closest = hit;
				objectHit = objects[i];
			}
		}
	}

Above is a part of my ray tracing program. It traces the ray and returns the computed colors to the kernel that it’s called from. However, when I tried debugging it, I noticed that the device function can’t get inside the for loop.

The “return Color(0, 1, 1);” line works fine when it’s on the position as above, producing a screen of green and blue mixture. But when I move this line inside the for loop like this:

for (int i = 0; i < OBJECTNUMBER; i++)
	{
		return Color(0, 1, 1);
		if (objects[i].intersect(ray, hit))
		{
			if (hit < closest)
			{
				closest = hit;
				objectHit = objects[i];
			}
		}
	}

This doesn’t work, returning a default black screen. How can I resolve this problem?

An update on the code:

__device__ Color trace(Ray &ray, int depth)
{
	float hit = INFINITY;
	float closest = INFINITY;
	Object objectHit;

	return Color(0, 1, 1);
	for (int i = 0; i < OBJECTNUMBER; i++)
	{
		if (objects[i].intersect(ray, hit))
		{
			if (hit < closest)
			{
				closest = hit;
				objectHit = objects[i];
			}
		}
	}

The objects and the lights arrays are set to device variables and are assigned to desired values at another kernel.

Initial problem where device kernel can’t do for loop is still there…

In which way is this a question related to OpenGL?
I can move the post to the CUDA programming topics if you inadvertently posted in the wrong section.

Are you saying it’s also not entering the for-loop if you remove line 7 in the last code snippet?

What’s the value of OBJECTNUMBER?

“However, when I tried debugging it, I noticed that the device function can’t get inside the for loop.”

Does it work when not debugging?

What if you eliminate the dead code inside the for-loop code where you put the return color inside?
E.g. like this.

for (int i = 0; i < OBJECTNUMBER; i++)
{
  return Color(0, 1, 1); // cyan
}
return Color(1, 0, 1); // magenta

Which color do you get?
If OBJECTNUMBER > 0 and the result is magenta, could you provide a minimal self-contained reproducer code for the CUDA compiler team to investigate?