refactor code generation for reducedim