When a gas is experiencing an irreversible expansion or compression, the rate of deformation of the gas is very rapid. When this happens (i.e., the process is irreversible), the behavior of the gas does not obey the ideal gas law, which is a thermodynamic equilibrium relationship. Instead, the behavior of the gas depends on the rate at which the gas is deforming. To give you an idea how this works, a very crude approximation to the behavior of an ideal gas in a cylinder under irreversible expansion or compression conditions can be written as:
$$\frac{nRT}{V}-2\frac{\eta}{V}\frac{dV}{dt}=P_{ext}\tag{1}$$where ##\eta## is the viscosity of the gas and ##P_{ext}## is the external force per unit area applied to the gas. Note the time derivative of the gas volume in the viscosity term in the equation.
Here is an example of how this plays out. Suppose you have an adiabatic expansion or compression, so that the amount of heat added or removed is zero. From the first law of thermodynamics applied to this system, we have (in differential form):
$$nC_v\frac{dT}{dt}=-P_{ext}\frac{dV}{dt}\tag{2}$$If we combine these two equations, we obtain:
$$nC_v\frac{dT}{dt}=-nRT\frac{d\ln{V}}{dt}+2\eta V\left(\frac{d\ln{V}}{dt}\right)^2\tag{3}$$If we divide this equation by T, we obtain:
$$nC_v\frac{d\ln{T}}{dt}+nR\frac{d\ln{V}}{dt}=2\frac{\eta}{T} V\left(\frac{d\ln{V}}{dt}\right)^2\tag{4}$$
But, for an ideal gas, the left hand side of this equation is the rate of change of entropy:
$$\frac{dS}{dt}=2\frac{\eta}{T} V\left(\frac{d\ln{V}}{dt}\right)^2\tag{5}$$Note that, because the rate of change of volume is squared on the right hand side of this equation, the right hand side of this equation is positive definite (and thus does not depend on whether the gas is expanding or compressing). So, according to this equation, in an adiabatic irreversible expansion or compression of an ideal gas in a cylinder, the rate of generation of entropy is always positive, and proportional to the square of the rate of change of volume.
Like I said, this development only very crude and approximate, but it contains the key physical mechanism (viscous dissipation) responsible for the generation of entropy.