Replace some sizeof-s with target specific constants #18245
Conversation
@@ -2139,7 +2139,7 @@ void Module::BuildStaticsOffsets(AllocMemTracker *pamTracker) | |||
(S_SIZE_T(2 * sizeof(DWORD))*(S_SIZE_T(dwNumTypes)+S_SIZE_T(1))))); | |||
|
|||
for (DWORD i = 0; i < dwIndex; i++) { | |||
pRegularStaticOffsets[i * 2 ] = dwGCHandles[0]*sizeof(OBJECTREF); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sizeof(OBJECTREF)
can be just TARGET_POINTER_SIZE
. We use TARGET_POINTER_SIZE
for object reference sizes in many places already, so a few more are not a concern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will replace with TARGET_POINTER_SIZE
instead
src/vm/crossbitness.h
Outdated
#endif // _TARGET_64BIT_ | ||
}; | ||
|
||
struct StringObject : public Object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see this used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it was needed before #17876
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. It was needed before for ObjSizeOf(StringObject)
in StringObject::GetSize
.
src/vm/crossbitness.h
Outdated
#ifndef CROSSBITNESS_H | ||
#define CROSSBITNESS_H | ||
|
||
struct CrossBitness |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather use explicitly manually defined sizes than trust the C++ compiler that it is going to layout the structures the way we expect.
Building upon approach I took in https://github.com/dotnet/coreclr/pull/17876/files .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jkotas I also though about this alternative. But then I would also need to replace all ObjSizeOf(Object)
and ObjSizeOf(ArrayBase)
expressions with some constants as well.
Is any standard name for sizeof(Object) + sizeof(ObjHeader)
?
I would call it |
Or |
BTW: How far do you think you are right now - are there things that you have working locally? |
@jkotas The x64_arm crossgen version in https://github.com/echesakovMSFT/coreclr/tree/CrossCrossGen compiles and runs (at least on Windows) and I can crossgen |
@jkotas Regarding placements such constants - would asmconstants.h be appropriate file? Or I can keep them in crossbitness.h (for 32 and 64 bit targets) ? |
I would probably just add them at the top of object.h before
|
src/vm/object.inl
Outdated
@@ -257,7 +257,7 @@ inline /* static */ unsigned ArrayBase::GetBoundsOffset(MethodTable* pMT) | |||
if (!pMT->IsMultiDimArray()) | |||
return(offsetof(ArrayBase, m_NumComponents)); | |||
_ASSERTE(pMT->GetInternalCorElementType() == ELEMENT_TYPE_ARRAY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also fix offsetof(ArrayBase, m_NumComponents)
a few lines above while you are on it. Replace it with OBJECT_SIZE /* offset(ArrayBase, m_NumComponents */
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks for catching this
After I replaced
However, such assertions would be valid only for non-crossbitness scenarios (so I can limit them to such scenarois only with ifdefs). @jkotas What do you think? |
You can add these static asserts somewhere if you would like. I am not really concerned about these particular constants. This is unlikely to change; and if it does change and the constants get out of sync by accident, it will be easy to notice because of nothing will work. |
* OBJHEADER_SIZE * OBJECT_SIZE * OBJECT_BASESIZE * ARRAYBASE_SIZE * ARRAYBASE_BASESIZE and use them instead of corresponding expressions * sizeof(ObjHeader) * sizeof(Object) * ObjSizeOf(Object) * sizeof(ArrayBase) * ObjSizeOf(ArrayBase)
@jkotas Agree, this assertions would not be that helpful. I rebased and squashed some commits and wait for green ci. Thanks for the help! |
* Replace sizeof expressions with target-specific constants: * sizeof(ObjHeader) -> OBJHEADER_SIZE * sizeof(Object) -> OBJECT_SIZE * ObjSizeOf(Object) -> OBJECT_BASESIZE * sizeof(ArrayBase) -> ARRAYBASE_SIZE * ObjSizeOf(ArrayBase) -> ARRAYBASE_BASESIZE * Remove ObjSizeOf macro * Use OBJECT_SIZE in ArrayBase::GetBoundsOffset * Cast ppObj to CORCOMPILE_GCREFMAP_TOKENS* before dereferencing in FakePromote * LOG2_PTRSIZE should depend on _TARGET_64BIT_ Commit migrated from dotnet/coreclr@de58676
The goal of this PR is to identify all places in VM using sizeof operator that should be made target-specific in order to support cross-bitness scenario #16513.
The proposal is to introduce CrossBitness_SizeOf operator to make such places explicit.CrossBitness_SizeOf operator satisfies the following conditions:
CrossBitness_SizeOf(X) == sizeof(Y) where Y is a "fake" structure created to describe layout of X on the target platform in crossbitness scenarios (i.e. x64_arm). Note that usually sizeof(X) != sizeof(Y).It's been proven experimentally that this subset of changes is needed in order to get outputs of x64_arm and arm_arm crossgens to be binary identical for S.P.C.dll and CoreFx assemblies.
This PR also includes other related changes:
LOG2_PTRSIZE
should depend on_TARGET_64BIT_
;FakePromote
andReportPointersFromValueType
.This PR is to initiate design discussion (at alternatives discussion) and not a final work.Update: the final solution is to replace identified
sizeof
expressions with target-specifics constants@jkotas PTAL